﻿/**********************************************
 * 功能名称：报表生成类。
编写人：  杨莹
建立时间： 杨 2008年11月15日
说明：
 

 **************************************************************/

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.Office.Interop.Excel;
using System.IO;
using Hisun.Models;
using Hisun.Controllers;
using System.Collections.Generic;

namespace Hisun.Controllers
{

    
    public class CostReport
    {

        //定义全局变量
        HisunDBDataContext db = new HisunDBDataContext();
        static Microsoft.Office.Interop.Excel.ApplicationClass myExcelApp; //Excel进程
        Microsoft.Office.Interop.Excel.Workbook myExcelBook;//Excel工作薄
        Microsoft.Office.Interop.Excel.Worksheet myExcelSheet;//Excel工作表



        //清空EXCEL应用、工作薄、工作表三个对象
        private void ClearThreeObject()
        {
            myExcelApp = null;
            myExcelBook = null;
            myExcelSheet = null;
        }


        private string[] ReturnfilePath(string UserName, string ModelReportName)
        {
            string[] MyfilePath = new string[] { "", "", "", "" };
            //建立样表路径，随机生成用户输出文件名，用户EXCEL表，用户HTML输出文件名
            MyfilePath[0] = HttpContext.Current.Server.MapPath(@"\Report\Sample\" + ModelReportName + ".xls");
            MyfilePath[1] = ModelReportName + UserName + "_" + DateTime.Now.GetHashCode().ToString();//ToString();      
            MyfilePath[2] = HttpContext.Current.Server.MapPath(@"\Report") + @"\UserReport\" + MyfilePath[1] + ".xls";
            MyfilePath[3] = HttpContext.Current.Server.MapPath(@"\Report") + @"\UserReport\" + MyfilePath[1] + ".html";

            if (!File.Exists(MyfilePath[0]))
            {
                //样表不存在
                MyfilePath[0] = "";
                MyfilePath[1] = "";
                MyfilePath[2] = "";
                MyfilePath[3] = "";


            }

            if (File.Exists(MyfilePath[2]))
            {
                File.Delete(MyfilePath[2]);
            }

            if (File.Exists(MyfilePath[3]))
            {
                File.Delete(MyfilePath[3]);
            }

            //挎出样表到临时输出表
            File.Copy(MyfilePath[0], MyfilePath[2]);

            return MyfilePath;

        }

        #region  取上一会计期间(PriorAccountPeriod)
        private string PriorAccountPeriod(string AccountPeriod)
        {
            string PriorAP = "";

            int Month = Convert.ToInt32(AccountPeriod.Substring(4, 2));

            if (Month == 1)
            {

                PriorAP = (Convert.ToInt32(AccountPeriod.Substring(0, 4)) - 1).ToString() + "12";
            }
            else
            {
                if (Month - 1 < 10)
                { PriorAP = AccountPeriod.Substring(0, 4) + "0" + (Month - 1).ToString(); }
                else
                { PriorAP = AccountPeriod.Substring(0, 4) + (Month - 1).ToString(); };
            };
            return PriorAP;

        }
#endregion 

        private void WriteReportStatistics(string RepName, string User, string Querystring, DateTime QueryTime)
        {
            var IsExist = from a in db.TABC_PUBLIC_ReportStatistics
                          where a.报表名称 == RepName
                          && a.操作员ID == User
                          && a.时间 == QueryTime
                          && a.查询内容 == Querystring
                          select a;
            if (IsExist.Count() == 0)
            {
                TABC_PUBLIC_ReportStatistics AddRow = new TABC_PUBLIC_ReportStatistics();
                AddRow.报表名称 = RepName;
                AddRow.操作员ID = User;
                AddRow.时间 = QueryTime;
                AddRow.查询内容 = Querystring;

                db.TABC_PUBLIC_ReportStatistics.InsertOnSubmit(AddRow);

                db.SubmitChanges();
            
            }
        
        
        
        }

        enum Letters
        {
            A = 1, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, AA, AB, AC
        };
        private class TempLetters
        {
            public string letter;//对应字母
            public string ID;//成本细项ID
            public int Number;//字母对应数字
            public string Name;//细项名称
            public decimal Money;//金额
        }

        #region 取当前会计期间的某资源 单位价格(UnitPrice)
        private decimal UnitPrice(string CostItem,string AccountPeriod)
        {
            decimal MyPrice = 1;  
            //2010年9月，该代码替换成由客户端维护的燃动计划价格表。
            //var QueryPrice = (from a in db.VABC_Report_ActiveUnitsCost
            //                 where a.会计期间ID == AccountPeriod
            //                 && a.成本细项ID == CostItem
            //                 select a).Single();

            var QueryPrice = from a in db.TABC_JS_CostItems_PlanPrice
                             where a.会计期间ID == AccountPeriod
                             && a.成本细项ID == CostItem
                             select a;
            if (QueryPrice.Count() == 0)
            {
               //如果为空的话，价格为1. 
            
            }

            if (QueryPrice.Count() == 1)
            { MyPrice = (decimal)QueryPrice.Single().计划价; }


            return MyPrice;

        }
#endregion

        #region 例子
        private void ExcelConvertToHtml(Workbook MyWorkbook, string htmlPath)
        {
            //转移当前EXCEL工作薄为HTML，保存HTML文件。
            object fileName = htmlPath;
            object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;//Html   
            object o = System.Reflection.Missing.Value;
            MyWorkbook.SaveAs(fileName, format, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o, o);
        }

        //一个制作报表的程序段。
        public string WriteExcel_Report_CenterList(int CenterId, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "交库成本明细表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];


            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {

                var myQuerys1 = (from a in db.TABC_JS_CostCenter where a.上级作业中心ID == CenterId select a).ToArray();

                if (myQuerys1.Count() == 0)
                {
                    myExcelSheet.Cells[5, 1] = "没有满足查询的记录";

                }
                else
                { 
                                
                    myExcelSheet.Cells[2, 2] = "hisun";
                    myExcelSheet.Cells[3, 2] = myQuerys1[0].上级作业中心ID.ToString();
                    myExcelSheet.Cells[3, 4] = "测试";
                    int mySumCount = myQuerys1.Count();
                    int StartRow = 6;
                
                    for (int i = 0; i < mySumCount; i++)                
                    {
                    myExcelSheet.Cells[StartRow + i, 1] = myQuerys1[i].作业中心ID.ToString();
                    myExcelSheet.Cells[StartRow + i, 2] = myQuerys1[i].作业中心名称.ToString();
                    myExcelSheet.Cells[StartRow + i, 3] = myQuerys1[i].类型.ToString();
                    myExcelSheet.Cells[StartRow + i, 4] = myQuerys1[i].是否直接成本.ToString();
                    myExcelSheet.Cells[StartRow + i, 5] = myQuerys1[i].成本细项ID.ToString();
                
                    }
                }



                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

#endregion

        #region 直接成本分配查询
        public string WriteExcel_Report_ZJCBFPCX(int CenterId, string AccountPeriod, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "直接成本分配查询表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics("作业成本分配查询表",UserName,CenterId.ToString()+AccountPeriod,DateTime.Now);


            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;

            int StartRow = 6;
            int j = 0;

            try
            {

                var myQuerys = from a in db.VABC_Report_ProductMainPanelCostPath where a.作业中心ID == CenterId && a.会计期间ID == AccountPeriod select a;
                               
                if (myQuerys.Count() == 0)
                {

                    myExcelSheet.Cells[6, 1] = "没有满足查询的记录";

                }
                else
                {
                    int InsertRowNum = myQuerys.Count();

                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);

                    }
                    // myRange.RowHeight = 19;
                    myRange = null;


                    myExcelSheet.Cells[3, 1] = "作业中心：(" + CenterId.ToString() + ")" + myQuerys.First().作业中心名称;
                    myExcelSheet.Cells[4, 1] = "会计期间：" + "(" + AccountPeriod + ")";
                    foreach (var MyRow in myQuerys)
                    {

                        myExcelSheet.Cells[StartRow + j, 1] = MyRow.目标中间体ID;
                        myExcelSheet.Cells[StartRow + j, 2] = MyRow.目标中间体名称;
                        myExcelSheet.Cells[StartRow + j, 3] = MyRow.目标产品ID;
                        myExcelSheet.Cells[StartRow + j, 4] = MyRow.成本细项名称;
                        myExcelSheet.Cells[StartRow + j, 5] = MyRow.数量;
                        myExcelSheet.Cells[StartRow + j, 6] = Math.Round((decimal)MyRow.金额, 2, MidpointRounding.AwayFromZero);

                        j++;

                    }


                }




                myExcelBook.Save();


                Range range = myExcelSheet.get_Range("A1".ToString(), Missing);
                Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");


                Range range_Printe = myExcelSheet.get_Range("B1".ToString(), Missing);
                Hyperlink hyperlink2 = (Hyperlink)range_Printe.Hyperlinks.Add(range_Printe, "/Report/UserReport/" + StrReportName + ".xls", Missing, Missing, "打印");

                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion


        #region 作业成本分配查询
        public string WriteExcel_Report_ZYCBFPCX(int CenterId,string AccountPeriod , string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "作业成本分配查询表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics("作业成本分配查询表",UserName,CenterId.ToString()+AccountPeriod,DateTime.Now);


            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;

            int StartRow = 6;
            int j = 0;
            string MyCostItemsFlag = "yes";
            int MyCount_self = 0;

            try
            {

                var myQuerys = from a in db.VABC_Report_MainPanelCostPath
                               join c in db.TABC_JS_CostCenter on a.作业中心ID equals c.作业中心ID
                               join b in db.TABC_DM_CostItems on a.成本细项ID equals b.成本细项ID
                               where a.会计期间ID == AccountPeriod
                               && a.源作业中心ID == CenterId
                               orderby c.作业中心名称
                               select new
                               {
                                 a.作业中心ID,
                                 c.作业中心名称,
                                 a.动因量,
                                 a.金额,
                                 a.数量,
                                 b.成本细项名称
                               };
                if (myQuerys.Count() == 0)
                { 
                
                  myQuerys =  from a in db.VABC_Report_MainPanelCostPath
                               join c in db.TABC_JS_CostCenter on a.作业中心ID equals c.作业中心ID
                               join b in db.TABC_DM_CostItems on a.成本细项ID equals b.成本细项ID
                               where a.会计期间ID == AccountPeriod
                               && a.作业中心ID == CenterId
                               orderby c.作业中心名称
                               select new
                               {
                                 a.作业中心ID,
                                 c.作业中心名称,
                                 a.动因量,
                                 a.金额,
                                 a.数量,
                                 b.成本细项名称
                               };
                 var   myQuerys_selfs = from d in db.TABC_JC_CostCenterItems
                                      join e in db.TABC_JS_CostCenter on d.作业中心ID equals e.作业中心ID
                                       join f in db.TABC_DM_CostItems on d.成本细项ID equals f.成本细项ID
                                       where d.会计期间ID == AccountPeriod
                                       && d.作业中心ID == CenterId
                                       orderby e.作业中心名称
                                    select new
                                    {
                                        d.作业中心ID,
                                        e.作业中心名称,
                                        动因量 = 0,
                                        d.金额,
                                        f.成本细项名称
                                    };

                  MyCostItemsFlag = "no";
                  MyCount_self = myQuerys_selfs.Count();
                
                }

                var MyTitleQuery1 = (from m in db.TABC_JS_CostCenter
                                   where m.作业中心ID == CenterId
                                   select new { m.作业中心ID, m.作业中心名称 }).Single();



                int InsertRowNum = myQuerys.Count() + MyCount_self;

                if (InsertRowNum == 0)
                {
                    myExcelSheet.Cells[5, 1] = "没有满足查询的记录";

                }
                else
                {           

                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                       
                    }
                   // myRange.RowHeight = 19;
                    myRange = null;


                    myExcelSheet.Cells[3, 1] = "作业中心：(" + MyTitleQuery1.作业中心ID.ToString() + ")" + MyTitleQuery1.作业中心名称;
                    myExcelSheet.Cells[4, 1] = "会计期间：" + "("+AccountPeriod+ ")";

                    if (MyCostItemsFlag == "yes")
                    {
                        var MyTitleQuery2 = ((from n in db.TABC_JS_CostCenterPathFramework
                                              join m in db.TABC_DM_CostDivers on n.成本动因ID equals m.成本动因ID
                                              where n.作业中心ID == CenterId
                                              select new { n.成本动因ID, m.成本动因名称 }).Distinct()).Single();

                        myExcelSheet.Cells[4, 3] = "分配动因：" + MyTitleQuery2.成本动因名称;
                    }

                    foreach (var MyRow in myQuerys)
                    {

                        myExcelSheet.Cells[StartRow + j, 1] = MyRow.作业中心ID;
                        myExcelSheet.Cells[StartRow + j, 2] = MyRow.作业中心名称;
                        myExcelSheet.Cells[StartRow + j, 3] = MyRow.成本细项名称;
                        myExcelSheet.Cells[StartRow + j, 4] = MyRow.动因量;
                        myExcelSheet.Cells[StartRow + j, 5] = MyRow.数量;
                        myExcelSheet.Cells[StartRow + j, 6] = Math.Round((decimal)MyRow.金额, 2, MidpointRounding.AwayFromZero);

                        j++;

                    }


                    if (MyCount_self != 0)
                    {

                        var myQuerys_selfs = from d in db.TABC_JC_CostCenterItems
                                             join e in db.TABC_JS_CostCenter on d.作业中心ID equals e.作业中心ID
                                             join f in db.TABC_DM_CostItems on d.成本细项ID equals f.成本细项ID
                                             where d.会计期间ID == AccountPeriod
                                             && d.作业中心ID == CenterId
                                             orderby e.作业中心名称
                                             select new
                                             {
                                                 d.作业中心ID,
                                                 e.作业中心名称,
                                                 动因量 = 0,
                                                 d.金额,
                                                 d.数量,
                                                 f.成本细项名称
                                             };

                        foreach (var myQuerys_self in myQuerys_selfs)
                        {

                            myExcelSheet.Cells[StartRow + j, 1] = myQuerys_self.作业中心ID;
                            myExcelSheet.Cells[StartRow + j, 2] = myQuerys_self.作业中心名称;
                            myExcelSheet.Cells[StartRow + j, 3] = myQuerys_self.成本细项名称;
                            myExcelSheet.Cells[StartRow + j, 4] = myQuerys_self.动因量;
                            myExcelSheet.Cells[StartRow + j, 5] = myQuerys_self.数量;
                            myExcelSheet.Cells[StartRow + j, 6] = Math.Round((decimal)myQuerys_self.金额, 2, MidpointRounding.AwayFromZero);

                            j++;

                        }
                    }


                }

       


                myExcelBook.Save();

                
                Range range = myExcelSheet.get_Range("A1".ToString(), Missing);
                Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");


                Range range_Printe = myExcelSheet.get_Range("B1".ToString(), Missing);
                Hyperlink hyperlink2 = (Hyperlink)range_Printe.Hyperlinks.Add(range_Printe, "/Report/UserReport/" + StrReportName + ".xls", Missing, Missing, "打印"); 
   
                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion

        #region 产品作业中心计划成本汇总表

        public string WriteExcel_Report_CPZYZXJHCBHZB(string MyProductId, string MyAp, string UserName, bool AddRealInventory, bool ConvertProductInput)
        {

            string[] Myfile = ReturnfilePath(UserName, "产品作业中心计划成本汇总表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics("产品作业中心计划成本汇总表", UserName, MyProductId + "-" + MyAp, DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;

            int MeterialFlag = 1;
            int ActivityFlag = 1;
            int j = 0;
            int k = 0;
            int n = 0;
            double ConvertIndex = 0;//用于保存还原率
            string ConvertMeterialID = "";//要还原的投料产品存货编码
            string ConvertProductID = "";//要还原的投料产品成本账户


            try
            {





                if (AddRealInventory == false)
                {
                    var MyMeterialQuerys = from a in db.TABC_Report_ProductMeterialsCost_Plan
                                           where a.产品ID == MyProductId
                                           && a.会计期间ID == MyAp
                                           orderby a.材料ID
                                           select a;
                    if (MyMeterialQuerys.Count() == 0)
                    {
                        //没有记录不做处理
                        MeterialFlag = 0;
                    }
                    else
                    {

                        //插入表头信息

                        var ProductInfo = MyMeterialQuerys.Take(1).Single();



                        myExcelSheet.Cells[3, 1] = "产品账户编码：(" + ProductInfo.产品ID.ToString() + ")";
                        myExcelSheet.Cells[4, 1] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                        myExcelSheet.Cells[3, 3] = "会计期间：(" + ProductInfo.会计期间ID.ToString() + ")";
                        myExcelSheet.Cells[4, 4] = ProductInfo.产品数量;
                        myExcelSheet.Cells[3, 7] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                        myExcelSheet.Cells[3, 11] = "产品产量：(" + ProductInfo.产品数量.ToString() + ")";


                         if (ConvertProductInput == true)  //当需要进行产品还原时
                         {
                             #region 当需要进行产品还原
                             var ConvertProductId = from a in MyMeterialQuerys
                                                    join b in db.TABC_JS_Product on a.材料ID equals b.产品ID
                                                    join c in db.TABC_Report_ProductMeterialsCost on b.产品账户ID equals c.产品ID
                                                    where c.会计期间ID == MyAp
                                                    select new
                                                    {
                                                        投料产品ID = a.材料ID,
                                                        成本账户ID = b.产品账户ID,
                                                        产品投料数量 = (a.投入数量 == null ? 0 : a.投入数量),
                                                        投料产品产量 = (c.产品数量 == null ? 0 : c.产品数量)

                                                    };
                              if (ConvertProductId.Count() > 0)
                             {
                                 var MyConvertInfo = ConvertProductId.Take(1).Single();

                                 ConvertMeterialID = MyConvertInfo.投料产品ID;
                                 ConvertProductID = MyConvertInfo.成本账户ID;
                                 if (MyConvertInfo.投料产品产量 > 0)
                                 {

                                     ConvertIndex = (double)MyConvertInfo.产品投料数量 / (double)MyConvertInfo.投料产品产量;
                                 }


                                 var NewMyMeterialQuerys = ((from a in db.TABC_Report_ProductMeterialsCost_Plan
                                                             where a.产品ID == MyProductId
                                                             && a.会计期间ID == MyAp && a.材料ID != ConvertMeterialID
                                                             orderby a.材料ID
                                                             select new
                                                             {
                                                                 材料ID = a.材料ID,
                                                                 材料名称 = a.材料名称,
                                                                 产品ID = a.产品ID,
                                                                 产品名称 = a.产品名称,
                                                                 产品数量 = a.产品数量,
                                                                 单价 = a.单价,
                                                                 会计期间ID = a.会计期间ID,
                                                                 计量单位 = a.计量单位,
                                                                 结存金额 = a.结存金额,
                                                                 结存数量 = a.结存数量,
                                                                 金额 = a.金额,
                                                                 期初金额 = a.期初金额,
                                                                 期初数量 = a.期初数量,
                                                                 数量 = a.数量,
                                                                 损失金额 = a.损失金额,
                                                                 损失数量 = a.损失数量,
                                                                 投入金额 = a.投入金额,
                                                                 投入数量 = a.投入数量,


                                                             }).Concat(from b in db.TABC_Report_ProductMeterialsCost_Plan
                                                                       where b.产品ID == ConvertProductID
                                                                       && b.会计期间ID == MyAp
                                                                       orderby b.材料ID
                                                                       select new
                                                                       {
                                                                           材料ID = b.材料ID,
                                                                           材料名称 = b.材料名称,
                                                                           产品ID = b.产品ID,
                                                                           产品名称 = b.产品名称,
                                                                           产品数量 = b.产品数量,
                                                                           单价 = b.单价,
                                                                           会计期间ID = b.会计期间ID,
                                                                           计量单位 = b.计量单位,
                                                                           结存金额 = b.结存金额 - b.结存金额,
                                                                           结存数量 = b.结存数量 - b.结存数量,
                                                                           金额 = b.金额 * ConvertIndex,
                                                                           期初金额 = b.期初金额 - b.期初金额,
                                                                           期初数量 = b.期初数量 - b.期初数量,
                                                                           数量 = b.数量 * ConvertIndex,
                                                                           损失金额 = b.损失金额 - b.损失金额,
                                                                           损失数量 = b.损失数量 - b.损失数量,
                                                                           投入金额 = ConvertIndex*b.金额,
                                                                           投入数量 = ConvertIndex * b.数量


                                                                       })).ToList();

                                 var MyMeterialQuerys_Convert = from c in NewMyMeterialQuerys
                                                                group c by c.材料ID into s
                                                                select new
                                                                {
                                                                    材料ID = s.Key,
                                                                    材料名称 = s.Max(p => p.材料名称),
                                                                    产品ID = s.Max(p => p.产品ID),
                                                                    产品名称 = s.Max(p => p.产品名称),
                                                                    产品数量 = s.Max(p => p.产品数量),
                                                                    单价 = s.Max(p => p.单价),
                                                                    会计期间ID = s.Max(p => p.会计期间ID),
                                                                    计量单位 = s.Max(p => p.计量单位),
                                                                    结存金额 = s.Sum(p => p.结存金额),
                                                                    结存数量 = s.Sum(p => p.结存数量),
                                                                    金额 = s.Sum(p => p.金额),
                                                                    期初金额 = s.Sum(p => p.期初金额),
                                                                    期初数量 = s.Sum(p => p.期初数量),
                                                                    数量 = s.Sum(p => p.数量),
                                                                    损失金额 = s.Sum(p => p.损失金额),
                                                                    损失数量 = s.Sum(p => p.损失数量),
                                                                    投入金额 = s.Sum(p => p.投入金额),
                                                                    投入数量 = s.Sum(p => p.投入数量)



                                                                };




                        int StarRow = 7;

                        int InsertRowNum = MyMeterialQuerys_Convert.Count();


                        Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[8, Missing];
                        for (int i = 0; i < InsertRowNum; i++)
                        {
                            myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                            myRange.RowHeight = 11;

                        }
                        myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                        myRange.RowHeight = 11;
                        myRange = null;



                        foreach (var MyMeterialQuery in MyMeterialQuerys_Convert)
                        {


                            myExcelSheet.Cells[StarRow + j, 1] = MyMeterialQuery.材料ID;
                            myExcelSheet.Cells[StarRow + j, 2] = MyMeterialQuery.材料名称;

                            myExcelSheet.Cells[StarRow + j, 3] = MyMeterialQuery.计量单位;
                            myExcelSheet.Cells[StarRow + j, 4] = MyMeterialQuery.期初数量;
                            myExcelSheet.Cells[StarRow + j, 5] = MyMeterialQuery.期初金额;
                            myExcelSheet.Cells[StarRow + j, 6] = MyMeterialQuery.投入数量;
                            myExcelSheet.Cells[StarRow + j, 7] = MyMeterialQuery.投入金额;
                            myExcelSheet.Cells[StarRow + j, 8] = MyMeterialQuery.结存数量;
                            myExcelSheet.Cells[StarRow + j, 9] = MyMeterialQuery.结存金额;
                            myExcelSheet.Cells[StarRow + j, 10] = MyMeterialQuery.数量;
                            myExcelSheet.Cells[StarRow + j, 11] = MyMeterialQuery.金额;
                            myExcelSheet.Cells[StarRow + j, 12] = MyMeterialQuery.损失数量;
                            myExcelSheet.Cells[StarRow + j, 13] = MyMeterialQuery.损失金额;


                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
                                "=if(iserror(" + "J" + (StarRow + j) + "/d4),0," + "(J" + (StarRow + j) + "/D4))";
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                                "=if(iserror(" + "K" + (StarRow + j) + "/d4),0," + "(K" + (StarRow + j) + "/D4))";

                            j++;



                        }



                             }
                             #endregion
                         }else  //不需要成本还原
                         {
                         
                            #region 填充原材料
                        int StarRow = 7;

                        int InsertRowNum = MyMeterialQuerys.Count();


                        Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[8, Missing];
                        for (int i = 0; i < InsertRowNum; i++)
                        {
                            myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                            myRange.RowHeight = 11;

                        }
                        myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                        myRange.RowHeight = 11;
                        myRange = null;



                        foreach (var MyMeterialQuery in MyMeterialQuerys)
                        {


                            myExcelSheet.Cells[StarRow + j, 1] = MyMeterialQuery.材料ID;
                            myExcelSheet.Cells[StarRow + j, 2] = MyMeterialQuery.材料名称;

                            myExcelSheet.Cells[StarRow + j, 3] = MyMeterialQuery.计量单位;
                            myExcelSheet.Cells[StarRow + j, 4] = MyMeterialQuery.期初数量;
                            myExcelSheet.Cells[StarRow + j, 5] = MyMeterialQuery.期初金额;
                            myExcelSheet.Cells[StarRow + j, 6] = MyMeterialQuery.投入数量;
                            myExcelSheet.Cells[StarRow + j, 7] = MyMeterialQuery.投入金额;
                            myExcelSheet.Cells[StarRow + j, 8] = MyMeterialQuery.结存数量;
                            myExcelSheet.Cells[StarRow + j, 9] = MyMeterialQuery.结存金额;
                            myExcelSheet.Cells[StarRow + j, 10] = MyMeterialQuery.数量;
                            myExcelSheet.Cells[StarRow + j, 11] = MyMeterialQuery.金额;
                            myExcelSheet.Cells[StarRow + j, 12] = MyMeterialQuery.损失数量;
                            myExcelSheet.Cells[StarRow + j, 13] = MyMeterialQuery.损失金额;


                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
                                "=if(iserror(" + "J" + (StarRow + j) + "/d4),0," + "(J" + (StarRow + j) + "/D4))";
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                                "=if(iserror(" + "K" + (StarRow + j) + "/d4),0," + "(K" + (StarRow + j) + "/D4))";

                            j++;



                        }
                         #endregion
                         }
                         

                    }
                }else
                {
                    var MyMeterialQuerys = from a in db.TABC_Report_ProductMeterialsCost_WithRealInventory_Plan
                                           where a.产品ID == MyProductId
                                           && a.会计期间ID == MyAp
                                           orderby a.材料ID
                                           select a;

                    if (MyMeterialQuerys.Count() == 0)
                    {
                        //没有记录不做处理
                        MeterialFlag = 0;

                    }
                    else
                    {

                        //插入表头信息

                        var ProductInfo = MyMeterialQuerys.Take(1).Single();

                        myExcelSheet.Cells[3, 1] = "产品账户编码：(" + ProductInfo.产品ID.ToString() + ")";
                        myExcelSheet.Cells[4, 1] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                        myExcelSheet.Cells[3, 3] = "会计期间：(" + ProductInfo.会计期间ID.ToString() + ")";
                        myExcelSheet.Cells[4, 4] = ProductInfo.产品数量;
                        myExcelSheet.Cells[3, 7] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                        myExcelSheet.Cells[3, 11] = "产品产量：(" + ProductInfo.产品数量.ToString() + ")";



                          if (ConvertProductInput == true)  //当需要进行产品还原时
                         {
                             #region 当需要进行产品还原
                             var ConvertProductId = from a in MyMeterialQuerys
                                                    join b in db.TABC_JS_Product on a.材料ID equals b.产品ID
                                                    join c in db.TABC_Report_ProductMeterialsCost on b.产品账户ID equals c.产品ID
                                                    where c.会计期间ID == MyAp
                                                    select new
                                                    {
                                                        投料产品ID = a.材料ID,
                                                        成本账户ID = b.产品账户ID,
                                                        产品投料数量 = (a.投入数量 == null ? 0 : a.投入数量),
                                                        投料产品产量 = (c.产品数量 == null ? 0 : c.产品数量)

                                                    };

                             if (ConvertProductId.Count() > 0)
                             {
                                 var MyConvertInfo = ConvertProductId.Take(1).Single();

                                 ConvertMeterialID = MyConvertInfo.投料产品ID;
                                 ConvertProductID = MyConvertInfo.成本账户ID;
                                 if (MyConvertInfo.投料产品产量 > 0)
                                 {

                                     ConvertIndex = (double)MyConvertInfo.产品投料数量 / (double)MyConvertInfo.投料产品产量;
                                 }


                                 var NewMyMeterialQuerys = ((from a in db.TABC_Report_ProductMeterialsCost_WithRealInventory_Plan
                                                             where a.产品ID == MyProductId
                                                             && a.会计期间ID == MyAp && a.材料ID != ConvertMeterialID
                                                             orderby a.材料ID
                                                             select new
                                                             {
                                                                 材料ID = a.材料ID,
                                                                 材料名称 = a.材料名称,
                                                                 产品ID = a.产品ID,
                                                                 产品名称 = a.产品名称,
                                                                 产品数量 = a.产品数量,
                                                                 单价 = a.单价,
                                                                 会计期间ID = a.会计期间ID,
                                                                 计量单位 = a.计量单位,
                                                                 结存金额 = a.结存金额,
                                                                 结存数量 = a.结存数量,
                                                                 金额 = a.金额,
                                                                 期初金额 = a.期初金额,
                                                                 期初数量 = a.期初数量,
                                                                 数量 = a.数量,
                                                                 损失金额 = a.损失金额,
                                                                 损失数量 = a.损失数量,
                                                                 投入金额 = a.投入金额,
                                                                 投入数量 = a.投入数量,


                                                             }).Concat(from b in db.TABC_Report_ProductMeterialsCost_Plan
                                                                       where b.产品ID == ConvertProductID
                                                                       && b.会计期间ID == MyAp
                                                                       orderby b.材料ID
                                                                       select new
                                                                       {
                                                                           材料ID = b.材料ID,
                                                                           材料名称 = b.材料名称,
                                                                           产品ID = b.产品ID,
                                                                           产品名称 = b.产品名称,
                                                                           产品数量 = (decimal)b.产品数量,
                                                                           单价 = (double)b.单价,
                                                                           会计期间ID = b.会计期间ID,
                                                                           计量单位 = b.计量单位,
                                                                           结存金额 = b.结存金额 - b.结存金额,
                                                                           结存数量 = b.结存数量 - b.结存数量,
                                                                           金额 = b.金额 * ConvertIndex,
                                                                           期初金额 = b.期初金额 - b.期初金额,
                                                                           期初数量 = b.期初数量 - b.期初数量,
                                                                           数量 = (double)b.数量 * ConvertIndex,
                                                                           损失金额 = b.损失金额 - b.损失金额,
                                                                           损失数量 = b.损失数量 - b.损失数量,
                                                                           投入金额 = ConvertIndex * b.金额,
                                                                           投入数量 =  ConvertIndex*b.数量 


                                                                       })).ToList();

                                 var MyMeterialQuerys_Convert = from c in NewMyMeterialQuerys
                                                                group c by c.材料ID into s
                                                                select new
                                                                {
                                                                    材料ID = s.Key,
                                                                    材料名称 = s.Max(p => p.材料名称),
                                                                    产品ID = s.Max(p => p.产品ID),
                                                                    产品名称 = s.Max(p => p.产品名称),
                                                                    产品数量 = s.Max(p => p.产品数量),
                                                                    单价 = s.Max(p => p.单价),
                                                                    会计期间ID = s.Max(p => p.会计期间ID),
                                                                    计量单位 = s.Max(p => p.计量单位),
                                                                    结存金额 = s.Sum(p => p.结存金额),
                                                                    结存数量 = s.Sum(p => p.结存数量),
                                                                    金额 = s.Sum(p => p.金额),
                                                                    期初金额 = s.Sum(p => p.期初金额),
                                                                    期初数量 = s.Sum(p => p.期初数量),
                                                                    数量 = s.Sum(p => p.数量),
                                                                    损失金额 = s.Sum(p => p.损失金额),
                                                                    损失数量 = s.Sum(p => p.损失数量),
                                                                    投入金额 = s.Sum(p => p.投入金额),
                                                                    投入数量 = s.Sum(p => p.投入数量)



                                                                };

                          #region 填充材料列表
                        int StarRow = 7;

                        int InsertRowNum = MyMeterialQuerys_Convert.Count();


                        Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[8, Missing];
                        for (int i = 0; i < InsertRowNum; i++)
                        {
                            myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                        }
                        myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                        myRange.RowHeight = 11;
                        myRange = null;


                        foreach (var MyMeterialQuery in MyMeterialQuerys_Convert)
                                 {


                                     myExcelSheet.Cells[StarRow + j, 1] = MyMeterialQuery.材料ID;
                                     myExcelSheet.Cells[StarRow + j, 2] = MyMeterialQuery.材料名称;

                                     myExcelSheet.Cells[StarRow + j, 3] = MyMeterialQuery.计量单位;
                                     myExcelSheet.Cells[StarRow + j, 4] = MyMeterialQuery.期初数量;
                                     myExcelSheet.Cells[StarRow + j, 5] = MyMeterialQuery.期初金额;
                                     myExcelSheet.Cells[StarRow + j, 6] = MyMeterialQuery.投入数量;
                                     myExcelSheet.Cells[StarRow + j, 7] = MyMeterialQuery.投入金额;
                                     myExcelSheet.Cells[StarRow + j, 8] = MyMeterialQuery.结存数量;
                                     myExcelSheet.Cells[StarRow + j, 9] = MyMeterialQuery.结存金额;
                                     myExcelSheet.Cells[StarRow + j, 10] = MyMeterialQuery.数量;
                                     myExcelSheet.Cells[StarRow + j, 11] = MyMeterialQuery.金额;
                                     myExcelSheet.Cells[StarRow + j, 12] = MyMeterialQuery.损失数量;
                                     myExcelSheet.Cells[StarRow + j, 13] = MyMeterialQuery.损失金额;


                                     ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
                                         "=if(iserror(" + "J" + (StarRow + j) + "/d4),0," + "(J" + (StarRow + j) + "/D4))";
                                     ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                                         "=if(iserror(" + "K" + (StarRow + j) + "/d4),0," + "(K" + (StarRow + j) + "/D4))";

                                     j++;



                                 }
                                 #endregion

                             }
                             #endregion

                    }else //不需要进行产品还原时

                             {
                               #region 填充材料列表
                             int StarRow = 7;

                             int InsertRowNum = MyMeterialQuerys.Count();


                             Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[8, Missing];
                             for (int i = 0; i < InsertRowNum; i++)
                             {
                                 myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                                 myRange.RowHeight = 11;

                             }
                             myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                             myRange.RowHeight = 11;
                             myRange = null;



                             foreach (var MyMeterialQuery in MyMeterialQuerys)
                             {


                                 myExcelSheet.Cells[StarRow + j, 1] = MyMeterialQuery.材料ID;
                                 myExcelSheet.Cells[StarRow + j, 2] = MyMeterialQuery.材料名称;

                                 myExcelSheet.Cells[StarRow + j, 3] = MyMeterialQuery.计量单位;
                                 myExcelSheet.Cells[StarRow + j, 4] = MyMeterialQuery.期初数量;
                                 myExcelSheet.Cells[StarRow + j, 5] = MyMeterialQuery.期初金额;
                                 myExcelSheet.Cells[StarRow + j, 6] = MyMeterialQuery.投入数量;
                                 myExcelSheet.Cells[StarRow + j, 7] = MyMeterialQuery.投入金额;
                                 myExcelSheet.Cells[StarRow + j, 8] = MyMeterialQuery.结存数量;
                                 myExcelSheet.Cells[StarRow + j, 9] = MyMeterialQuery.结存金额;
                                 myExcelSheet.Cells[StarRow + j, 10] = MyMeterialQuery.数量;
                                 myExcelSheet.Cells[StarRow + j, 11] = MyMeterialQuery.金额;
                                 myExcelSheet.Cells[StarRow + j, 12] = MyMeterialQuery.损失数量;
                                 myExcelSheet.Cells[StarRow + j, 13] = MyMeterialQuery.损失金额;


                                 ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
                                     "=if(iserror(" + "J" + (StarRow + j) + "/d4),0," + "(J" + (StarRow + j) + "/D4))";
                                 ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                                     "=if(iserror(" + "K" + (StarRow + j) + "/d4),0," + "(K" + (StarRow + j) + "/D4))";

                                 j++;



                             }
                             #endregion
                             
                             
                             }


                }



                }

//填充燃动部分
                var MyActivityCostQuerys = from a in db.TABC_Report_ProductActiveCost
                                           join b in db.TABC_DM_CostItems on a.成本细项Id equals b.成本细项ID
                                           where a.产品ID == MyProductId && a.会计期间ID == MyAp && b.参数描述 == "DL"
                                           orderby a.成本细项Id
                                           select new
                                           {
                                               a.产品ID,
                                                        a.产品名称,
                                                        a.产品数量,
                                                        a.成本细项Id,
                                                        a.成本细项名称,
                                                        a.会计期间ID,
                                                        a.结存金额,
                                                        a.结存数量,
                                                        a.金额,
                                                        a.数量,
                                                        a.期初金额,
                                                        a.期初数量,
                                                        a.损失金额,
                                                        a.损失数量,
                                                        a.投入金额,
                                                        a.投入数量
                                           };
                  if (ConvertProductInput == true)  //当需要进行产品还原时
                 {
                     #region 当需要进行产品还原

                         var NewActivityCostQuerys = (from a in db.TABC_Report_ProductActiveCost
                                                      join b in db.TABC_DM_CostItems on a.成本细项Id equals b.成本细项ID
                                                     where a.产品ID == MyProductId
                                                     && a.会计期间ID == MyAp && b.参数描述 == "DL"
                                                     orderby a.成本细项Id
                                                     select new
                                                     {
                                                        a.产品ID,
                                                        a.产品名称,
                                                        a.产品数量,
                                                        a.成本细项Id,
                                                        a.成本细项名称,
                                                        a.会计期间ID,
                                                        a.结存金额,
                                                        a.结存数量,
                                                        a.金额,
                                                        a.数量,
                                                        a.期初金额,
                                                        a.期初数量,
                                                        a.损失金额,
                                                        a.损失数量,
                                                        a.投入金额,
                                                        a.投入数量


                                                     }).Concat(from d in db.TABC_Report_ProductActiveCost
                                                                 join e in db.TABC_DM_CostItems on d.成本细项Id equals e.成本细项ID
                                                               where d.产品ID == ConvertProductID
                                                               && d.会计期间ID == MyAp && e.参数描述 == "DL"
                                                              orderby d.成本细项Id
                                                               select new
                                                               {

                                                        d.产品ID,
                                                        d.产品名称,
                                                        d.产品数量,
                                                        d.成本细项Id,
                                                        d.成本细项名称,
                                                        d.会计期间ID,
                                                        d.结存金额,
                                                        d.结存数量,
                                                        金额 =d.金额 * Convert.ToDecimal(ConvertIndex),
                                                        数量 = d.数量 * Convert.ToDecimal(ConvertIndex),
                                                        d.期初金额,
                                                        d.期初数量,
                                                        d.损失金额,
                                                        d.损失数量,
                                                        投入金额 = Convert.ToDecimal(ConvertIndex)*d.金额,
                                                        投入数量 = Convert.ToDecimal(ConvertIndex)*d.数量
                                                                                  });

                         var MyActivityCos_Convert = from c in NewActivityCostQuerys
                                                        group c by c.成本细项Id into s
                                                        select new
                                                        {
                                                            成本细项Id = s.Key,
                                                            产品ID = s.Max(p => p.产品ID),
                                                            成本细项名称 = s.Max(p =>p.成本细项名称),
                                                            产品名称 = s.Max(p => p.产品名称),
                                                            产品数量 = s.Max(p => p.产品数量),
                                                            会计期间ID = s.Max(p => p.会计期间ID),
                                                            结存金额 = s.Sum(p => p.结存金额),
                                                            结存数量 = s.Sum(p => p.结存数量),
                                                            金额 = s.Sum(p => p.金额),
                                                             数量 = s.Sum(p => p.数量),
                                                            期初金额 = s.Sum(p => p.期初金额),
                                                            期初数量 = s.Sum(p => p.期初数量),
                                                            损失金额 = s.Sum(p => p.损失金额),
                                                            损失数量 = s.Sum(p => p.损失数量),
                                                            投入金额 = s.Sum(p => p.投入金额),
                                                            投入数量 = s.Sum(p => p.投入数量)



                                                        };


                       string MyPriorAp = PriorAccountPeriod(MyAp);

                 if (MyActivityCos_Convert.Count() == 0)
                 {
                     //没有记录不做处理
                     ActivityFlag = 0;
                 }
                 else
                 {
                     int StarRow = 11 + j;
                     //插入表头信息
                     int InsertRowNum = MyActivityCos_Convert.Count();
                     Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow + 1, Missing];

                     for (int i = 0; i < InsertRowNum; i++)
                     {
                         range1.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                         //range1.RowHeight = 11;
                     }
                     range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];
                     range1.RowHeight = 11;
                     range1 = null;

                     var ProductInfo = MyActivityCostQuerys.Take(1).Single();


                     myExcelSheet.Cells[3, 1] = "产品账户编码：(" + ProductInfo.产品ID.ToString() + ")";
                     myExcelSheet.Cells[4, 1] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                     myExcelSheet.Cells[3, 3] = "会计期间：(" + ProductInfo.会计期间ID.ToString() + ")";
                     myExcelSheet.Cells[4, 4] = ProductInfo.产品数量;
                     myExcelSheet.Cells[3, 7] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                     myExcelSheet.Cells[3, 11] = "产品产量：(" + ProductInfo.产品数量.ToString() + ")";
                     foreach (var MyActivityCostRow in MyActivityCos_Convert)
                     {
                         string QueryDetailName = MyActivityCostRow.成本细项名称;
                         myExcelSheet.Cells[StarRow + k, 2] = QueryDetailName;
                         myExcelSheet.Cells[StarRow + k, 5] = MyActivityCostRow.期初金额;
                         myExcelSheet.Cells[StarRow + k, 7] = MyActivityCostRow.投入金额;
                         myExcelSheet.Cells[StarRow + k, 9] = MyActivityCostRow.结存金额;
                         myExcelSheet.Cells[StarRow + k, 11] = MyActivityCostRow.金额;
                         myExcelSheet.Cells[StarRow + k, 13] = MyActivityCostRow.损失金额;      
                     myExcelSheet.Cells[StarRow + k, 4] = MyActivityCostRow.期初数量;
                         myExcelSheet.Cells[StarRow + k, 6] = MyActivityCostRow.投入数量;              
                             myExcelSheet.Cells[StarRow, 8] = MyActivityCostRow.结存数量;
                               myExcelSheet.Cells[StarRow, 12] = MyActivityCostRow.损失数量;
                         myExcelSheet.Cells[StarRow, 10] = MyActivityCostRow.数量;
                         
                         int TempRow = StarRow + k;
                         k++;
                     }

                 }


                     #endregion
                 }else
                 {
                          #region 不需要产品还原
                             if (MyActivityCostQuerys.Count() == 0)
                             {
                                 //没有记录不做处理
                                 ActivityFlag = 0;
                             }
                                 else
                                 {
                                     int StarRow = 11 + j;
                                     //插入表头信息
                                     int InsertRowNum = MyActivityCostQuerys.Count();
                                     Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow + 1, Missing];

                                     for (int i = 0; i < InsertRowNum; i++)
                                     {
                                         range1.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                                         //range1.RowHeight = 11;
                                     }
                                     range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];
                                     range1.RowHeight = 11;
                                     range1 = null;

                                     var ProductInfo = MyActivityCostQuerys.Take(1).Single();


                                     myExcelSheet.Cells[3, 1] = "产品账户编码：(" + ProductInfo.产品ID.ToString() + ")";
                                     myExcelSheet.Cells[4, 1] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                                     myExcelSheet.Cells[3, 3] = "会计期间：(" + ProductInfo.会计期间ID.ToString() + ")";
                                     myExcelSheet.Cells[4, 4] = ProductInfo.产品数量;
                                     myExcelSheet.Cells[3, 7] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                                     myExcelSheet.Cells[3, 11] = "产品产量：(" + ProductInfo.产品数量.ToString() + ")";
                                     foreach (var MyActivityCostRow in MyActivityCostQuerys)
                                     {
                                         string QueryDetailName = MyActivityCostRow.成本细项名称;
                                         myExcelSheet.Cells[StarRow + k, 2] = QueryDetailName;
                                         myExcelSheet.Cells[StarRow + k, 5] = MyActivityCostRow.期初金额;
                                         myExcelSheet.Cells[StarRow + k, 7] = MyActivityCostRow.投入金额;
                                         myExcelSheet.Cells[StarRow + k, 9] = MyActivityCostRow.结存金额;
                                         myExcelSheet.Cells[StarRow + k, 11] = MyActivityCostRow.金额;
                                         myExcelSheet.Cells[StarRow + k, 13] = MyActivityCostRow.损失金额;      
                                     myExcelSheet.Cells[StarRow + k, 4] = MyActivityCostRow.期初数量;
                                         myExcelSheet.Cells[StarRow + k, 6] = MyActivityCostRow.投入数量;              
                                             myExcelSheet.Cells[StarRow, 8] = MyActivityCostRow.结存数量;
                                               myExcelSheet.Cells[StarRow, 12] = MyActivityCostRow.损失数量;
                                         myExcelSheet.Cells[StarRow, 10] = MyActivityCostRow.数量;
                                         
                                         int TempRow = StarRow + k;
                                         k++;
                                     }
             

                             }      
                          #endregion        
                 }



                var MyActivityCostQuerys_ZY = from a in db.TABC_Report_ProductActiveCost
                                              join b in db.TABC_DM_CostItems on a.成本细项Id equals b.成本细项ID
                                              where a.产品ID == MyProductId && a.会计期间ID == MyAp && b.参数描述 == "ZY"
                                              orderby a.成本细项Id
                                              select new
                                              {
                                                  a.产品ID,
                                                        a.产品名称,
                                                        a.产品数量,
                                                        a.成本细项Id,
                                                        a.成本细项名称,
                                                        a.会计期间ID,
                                                        a.结存金额,
                                                        a.结存数量,
                                                        a.金额,
                                                        a.数量,
                                                        a.期初金额,
                                                        a.期初数量,
                                                        a.损失金额,
                                                        a.损失数量,
                                                        a.投入金额,
                                                        a.投入数量
                                              };

                if (ConvertProductInput == true)  //当需要进行产品还原时
                 {
                     #region 当需要进行产品还原

                     var NewActivityCostQuerys = (from a in db.TABC_Report_ProductActiveCost
                                                  join b in db.TABC_DM_CostItems on a.成本细项Id equals b.成本细项ID
                                                  where a.产品ID == MyProductId
                                                  && a.会计期间ID == MyAp && b.参数描述 == "ZY"
                                                  orderby a.成本细项Id
                                                  select new
                                                  {
                                                      a.产品ID,
                                                      a.产品名称,
                                                      a.产品数量,
                                                      a.成本细项Id,
                                                      a.成本细项名称,
                                                      a.会计期间ID,
                                                      a.结存金额,
                                                      a.结存数量,
                                                      a.金额,
                                                      a.数量,
                                                      a.期初金额,
                                                      a.期初数量,
                                                      a.损失金额,
                                                      a.损失数量,
                                                      a.投入金额,
                                                      a.投入数量


                                                  }).Concat(from d in db.TABC_Report_ProductActiveCost
                                                            join e in db.TABC_DM_CostItems on d.成本细项Id equals e.成本细项ID
                                                            where d.产品ID == ConvertProductID
                                                            && d.会计期间ID == MyAp && e.参数描述 == "ZY"
                                                            orderby d.成本细项Id
                                                            select new
                                                            {

                                                                d.产品ID,
                                                                d.产品名称,
                                                                d.产品数量,
                                                                d.成本细项Id,
                                                                d.成本细项名称,
                                                                d.会计期间ID,
                                                                d.结存金额,
                                                                d.结存数量,
                                                                金额 = d.金额 * Convert.ToDecimal(ConvertIndex),
                                                                数量 = d.数量 * Convert.ToDecimal(ConvertIndex),
                                                                d.期初金额,
                                                                d.期初数量,
                                                                d.损失金额,
                                                                d.损失数量,
                                                                投入金额 = Convert.ToDecimal(ConvertIndex) * d.金额,
                                                                投入数量 = Convert.ToDecimal(ConvertIndex)*d.数量
                                                            });

                     var MyActivityCos_Convert = from c in NewActivityCostQuerys
                                                 group c by c.成本细项Id into s
                                                 select new
                                                 {
                                                     成本细项Id = s.Key,
                                                     产品ID = s.Max(p => p.产品ID),
                                                     成本细项名称 = s.Max(p => p.成本细项名称),
                                                     产品名称 = s.Max(p => p.产品名称),
                                                     产品数量 = s.Max(p => p.产品数量),
                                                     会计期间ID = s.Max(p => p.会计期间ID),
                                                     结存金额 = s.Sum(p => p.结存金额),
                                                     结存数量 = s.Sum(p => p.结存数量),
                                                     金额 = s.Sum(p => p.金额),
                                                     数量 = s.Sum(p => p.数量),
                                                     期初金额 = s.Sum(p => p.期初金额),
                                                     期初数量 = s.Sum(p => p.期初数量),
                                                     损失金额 = s.Sum(p => p.损失金额),
                                                     损失数量 = s.Sum(p => p.损失数量),
                                                     投入金额 = s.Sum(p => p.投入金额),
                                                     投入数量 = s.Sum(p => p.投入数量)



                                                 };


                     // string MyPriorAp = PriorAccountPeriod(MyAp);

                     if (MyActivityCos_Convert.Count() == 0)
                     {
                         //没有记录不做处理
                         ActivityFlag = 0;
                     }
                     else
                     {
                         int StarRow = 16 + j + k;
                         //插入表头信息
                         int InsertRowNum = MyActivityCos_Convert.Count();
                         Microsoft.Office.Interop.Excel.Range range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];

                         for (int i = 0; i < InsertRowNum; i++)
                         {
                             range2.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                             //range1.RowHeight = 11;
                         }
                         range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow + 1, Missing];
                         range2.RowHeight = 11;
                         range2 = null;

                         var ProductInfo = MyActivityCostQuerys_ZY.Take(1).Single();


                         myExcelSheet.Cells[3, 1] = "产品账户编码：(" + ProductInfo.产品ID.ToString() + ")";
                         myExcelSheet.Cells[4, 1] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                         myExcelSheet.Cells[3, 3] = "会计期间：(" + ProductInfo.会计期间ID.ToString() + ")";
                         myExcelSheet.Cells[4, 4] = ProductInfo.产品数量;
                         myExcelSheet.Cells[3, 7] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                         myExcelSheet.Cells[3, 11] = "产品产量：(" + ProductInfo.产品数量.ToString() + ")";

                         StarRow = StarRow - 1;
                         foreach (var MyActivityCostRow_ZY in MyActivityCos_Convert)
                         {

                             string QueryDetailName = MyActivityCostRow_ZY.成本细项名称;
                             myExcelSheet.Cells[StarRow +n , 2] = QueryDetailName;
                             myExcelSheet.Cells[StarRow + n, 5] = MyActivityCostRow_ZY.期初金额;
                             myExcelSheet.Cells[StarRow + n, 7] = MyActivityCostRow_ZY.投入金额;
                             myExcelSheet.Cells[StarRow + n, 9] = MyActivityCostRow_ZY.结存金额;
                             myExcelSheet.Cells[StarRow + n, 11] = MyActivityCostRow_ZY.金额;
                             myExcelSheet.Cells[StarRow + n, 13] = MyActivityCostRow_ZY.损失金额;
                             myExcelSheet.Cells[StarRow + n, 4] = MyActivityCostRow_ZY.期初数量;
                             myExcelSheet.Cells[StarRow + n, 6] = MyActivityCostRow_ZY.投入数量;
                             myExcelSheet.Cells[StarRow+n, 8] = MyActivityCostRow_ZY.结存数量;
                             myExcelSheet.Cells[StarRow+n, 12] = MyActivityCostRow_ZY.损失数量;
                             myExcelSheet.Cells[StarRow+n, 10] = MyActivityCostRow_ZY.数量;

                             n++;



                         }

                     }


                     #endregion
                 }
                 else
                 {
                     #region 不需要产品还原
                     if (MyActivityCostQuerys_ZY.Count() == 0)
                     {
                         //没有记录不做处理
                         ActivityFlag = 0;
                     }
                     else
                     {
                         int StarRow = 16 + j+k;
                         //插入表头信息
                         int InsertRowNum = MyActivityCostQuerys_ZY.Count();
                         Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];

                         for (int i = 0; i < InsertRowNum; i++)
                         {
                             range1.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                             //range1.RowHeight = 11;
                         }
                         range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];
                         range1.RowHeight = 11;
                         range1 = null;

                         var ProductInfo = MyActivityCostQuerys_ZY.Take(1).Single();


                         myExcelSheet.Cells[3, 1] = "产品账户编码：(" + ProductInfo.产品ID.ToString() + ")";
                         myExcelSheet.Cells[4, 1] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                         myExcelSheet.Cells[3, 3] = "会计期间：(" + ProductInfo.会计期间ID.ToString() + ")";
                         myExcelSheet.Cells[4, 4] = ProductInfo.产品数量;
                         myExcelSheet.Cells[3, 7] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                         myExcelSheet.Cells[3, 11] = "产品产量：(" + ProductInfo.产品数量.ToString() + ")";

                         StarRow = StarRow - 1;
                         foreach (var MyActivityCostRow in MyActivityCostQuerys_ZY)
                         {
                             string QueryDetailName = MyActivityCostRow.成本细项名称;
                             myExcelSheet.Cells[StarRow + n, 2] = QueryDetailName;
                             myExcelSheet.Cells[StarRow + n, 5] = MyActivityCostRow.期初金额;
                             myExcelSheet.Cells[StarRow + n, 7] = MyActivityCostRow.投入金额;
                             myExcelSheet.Cells[StarRow + n, 9] = MyActivityCostRow.结存金额;
                             myExcelSheet.Cells[StarRow + n, 11] = MyActivityCostRow.金额;
                             myExcelSheet.Cells[StarRow + n, 13] = MyActivityCostRow.损失金额;
                             myExcelSheet.Cells[StarRow + n, 4] = MyActivityCostRow.期初数量;
                             myExcelSheet.Cells[StarRow + n, 6] = MyActivityCostRow.投入数量;
                             myExcelSheet.Cells[StarRow + n, 8] = MyActivityCostRow.结存数量;
                             myExcelSheet.Cells[StarRow + n, 12] = MyActivityCostRow.损失数量;
                             myExcelSheet.Cells[StarRow + n, 10] = MyActivityCostRow.数量;

                             n++;

                         }
                     #endregion

                     }
                 }

                 if (MyActivityCostQuerys.Count() == 0)
                 {
                     //没有记录不做处理
                     ActivityFlag = 0;
                 }



                if (MeterialFlag + ActivityFlag == 0)
                {
                    myExcelSheet.Cells[3, 11] = "不存在作业成本";
                }

                myExcelBook.Save();


                Range range = myExcelSheet.get_Range("A1".ToString(), Missing);
                Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");


                Range range_Printe = myExcelSheet.get_Range("B1".ToString(), Missing);
                Hyperlink hyperlink2 = (Hyperlink)range_Printe.Hyperlinks.Add(range_Printe, "/Report/UserReport/" + StrReportName + ".xls", Missing, Missing, "打印");


                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion

         #region 产品作业中心成本汇总表

         public string WriteExcel_Report_CPZYZXCBHZB(string MyProductId, string MyAp, string UserName, bool AddRealInventory, string IsBaseReport,bool ConvertProductInput)
         {

             string[] Myfile = ReturnfilePath(UserName, (IsBaseReport == "yes" ? "产品作业中心成本汇总表_Base" : "产品作业中心成本汇总表"));
             string StrSamepleFile = Myfile[0];
             string StrReportName = Myfile[1];
             string StrReportFile = Myfile[2];
             string StrReportFileHtml = Myfile[3];

             //写入报表点击查询页面
             WriteReportStatistics((IsBaseReport == "yes" ? "产品作业中心成本汇总表_Base" : "产品作业中心成本汇总表"), UserName, MyProductId + "-" + MyAp, DateTime.Now);

             ClearThreeObject();


             object Missing = System.Reflection.Missing.Value;
             myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
             myExcelApp.Visible = false;
             myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
             myExcelBook = myExcelApp.Workbooks[1];
             myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;

             int MeterialFlag = 1;
             int ActivityFlag = 1;
             int j = 0;
             int k = 0;
             int n = 0;
             double ConvertIndex = 0;//用于保存还原率
             string ConvertMeterialID = "";//要还原的投料产品存货编码
             string ConvertProductID = "";//要还原的投料产品成本账户

             try
             {





                 if (AddRealInventory == false)
                 {
                     var MyMeterialQuerys = from a in db.TABC_Report_ProductMeterialsCost
                                            where a.产品ID == MyProductId
                                            && a.会计期间ID == MyAp
                                            orderby a.材料ID
                                            select a;
                     if (MyMeterialQuerys.Count() == 0)
                     {
                         //没有记录不做处理
                         MeterialFlag = 0;
                     }
                     else
                     {

                         
                         //插入表头信息

                         var ProductInfo = MyMeterialQuerys.Take(1).Single();
                         myExcelSheet.Cells[3, 1] = "产品账户编码：(" + ProductInfo.产品ID.ToString() + ")";
                         myExcelSheet.Cells[4, 1] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                         myExcelSheet.Cells[3, 3] = "会计期间：(" + ProductInfo.会计期间ID.ToString() + ")";
                         myExcelSheet.Cells[4, 4] = ProductInfo.产品数量;
                         myExcelSheet.Cells[3, 7] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                         myExcelSheet.Cells[3, 11] = "产品产量：(" + ProductInfo.产品数量.ToString() + ")";

                         if (ConvertProductInput == true)  //当需要进行产品还原时
                         {
                             #region 当需要进行产品还原
                             var ConvertProductId = from a in MyMeterialQuerys
                                                    join b in db.TABC_JS_Product on a.材料ID equals b.产品ID
                                                    join c in db.TABC_Report_ProductMeterialsCost on b.产品账户ID equals c.产品ID
                                                    where c.会计期间ID == MyAp
                                                    select new
                                                    {
                                                        投料产品ID = a.材料ID,
                                                        成本账户ID = b.产品账户ID,
                                                        产品投料数量 = (a.投入数量 == null ? 0 : a.投入数量),
                                                        投料产品产量 = (c.产品数量 == null ? 0 : c.产品数量)

                                                    };

                             if (ConvertProductId.Count() > 0)
                             {
                                 var MyConvertInfo = ConvertProductId.Take(1).Single();

                                 ConvertMeterialID = MyConvertInfo.投料产品ID;
                                 ConvertProductID = MyConvertInfo.成本账户ID;
                                 if (MyConvertInfo.投料产品产量 > 0)
                                 {

                                     ConvertIndex = (double)MyConvertInfo.产品投料数量 / (double)MyConvertInfo.投料产品产量;
                                 }


                                 var NewMyMeterialQuerys = ((from a in db.TABC_Report_ProductMeterialsCost
                                                             where a.产品ID == MyProductId
                                                             && a.会计期间ID == MyAp && a.材料ID != ConvertMeterialID
                                                             orderby a.材料ID
                                                             select new
                                                             {
                                                                 材料ID = a.材料ID,
                                                                 材料名称 = a.材料名称,
                                                                 产品ID = a.产品ID,
                                                                 产品名称 = a.产品名称,
                                                                 产品数量 = a.产品数量,
                                                                 单价 = a.单价,
                                                                 会计期间ID = a.会计期间ID,
                                                                 计量单位 = a.计量单位,
                                                                 结存金额 = a.结存金额,
                                                                 结存数量 = a.结存数量,
                                                                 金额 = a.金额,
                                                                 期初金额 = a.期初金额,
                                                                 期初数量 = a.期初数量,
                                                                 数量 = a.数量,
                                                                 损失金额 = a.损失金额,
                                                                 损失数量 = a.损失数量,
                                                                 投入金额 = a.投入金额,
                                                                 投入数量 = a.投入数量,


                                                             }).Concat(from b in db.TABC_Report_ProductMeterialsCost
                                                                       where b.产品ID == ConvertProductID
                                                                       && b.会计期间ID == MyAp
                                                                       orderby b.材料ID
                                                                       select new
                                                                       {
                                                                           材料ID = b.材料ID,
                                                                           材料名称 = b.材料名称,
                                                                           产品ID = b.产品ID,
                                                                           产品名称 = b.产品名称,
                                                                           产品数量 = b.产品数量,
                                                                           单价 = b.单价,
                                                                           会计期间ID = b.会计期间ID,
                                                                           计量单位 = b.计量单位,
                                                                           结存金额 = b.结存金额 - b.结存金额,
                                                                           结存数量 = b.结存数量 - b.结存数量,
                                                                           金额 = b.金额 * Convert.ToDecimal(ConvertIndex),
                                                                           期初金额 = b.期初金额 - b.期初金额,
                                                                           期初数量 = b.期初数量 - b.期初数量,
                                                                           数量 = b.数量 * ConvertIndex,
                                                                           损失金额 = b.损失金额 - b.损失金额,
                                                                           损失数量 = b.损失数量 - b.损失数量,
                                                                           投入金额 = Convert.ToDecimal(ConvertIndex)*b.金额,
                                                                           投入数量 = ConvertIndex * b.数量


                                                                       })).ToList();

                                 var MyMeterialQuerys_Convert = from c in NewMyMeterialQuerys
                                                                group c by c.材料ID into s
                                                                select new
                                                                {
                                                                    材料ID = s.Key,
                                                                    材料名称 = s.Max(p => p.材料名称),
                                                                    产品ID = s.Max(p => p.产品ID),
                                                                    产品名称 = s.Max(p => p.产品名称),
                                                                    产品数量 = s.Max(p => p.产品数量),
                                                                    单价 = s.Max(p => p.单价),
                                                                    会计期间ID = s.Max(p => p.会计期间ID),
                                                                    计量单位 = s.Max(p => p.计量单位),
                                                                    结存金额 = s.Sum(p => p.结存金额),
                                                                    结存数量 = s.Sum(p => p.结存数量),
                                                                    金额 = s.Sum(p => p.金额),
                                                                    期初金额 = s.Sum(p => p.期初金额),
                                                                    期初数量 = s.Sum(p => p.期初数量),
                                                                    数量 = s.Sum(p => p.数量),
                                                                    损失金额 = s.Sum(p => p.损失金额),
                                                                    损失数量 = s.Sum(p => p.损失数量),
                                                                    投入金额 = s.Sum(p => p.投入金额),
                                                                    投入数量 = s.Sum(p => p.投入数量)



                                                                };

                                 #region 填充材料列表
                                 int StarRow = 7;

                                 int InsertRowNum = MyMeterialQuerys_Convert.Count();


                                 Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[8, Missing];
                                 for (int i = 0; i < InsertRowNum; i++)
                                 {
                                     myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                                     myRange.RowHeight = 11;

                                 }
                                 myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                                 myRange.RowHeight = 11;
                                 myRange = null;



                                 foreach (var MyMeterialQuery in MyMeterialQuerys_Convert)
                                 {


                                     myExcelSheet.Cells[StarRow + j, 1] = MyMeterialQuery.材料ID;
                                     myExcelSheet.Cells[StarRow + j, 2] = MyMeterialQuery.材料名称;

                                     myExcelSheet.Cells[StarRow + j, 3] = MyMeterialQuery.计量单位;
                                     myExcelSheet.Cells[StarRow + j, 4] = MyMeterialQuery.期初数量;
                                     myExcelSheet.Cells[StarRow + j, 5] = MyMeterialQuery.期初金额;
                                     myExcelSheet.Cells[StarRow + j, 6] = MyMeterialQuery.投入数量;
                                     myExcelSheet.Cells[StarRow + j, 7] = MyMeterialQuery.投入金额;
                                     myExcelSheet.Cells[StarRow + j, 8] = MyMeterialQuery.结存数量;
                                     myExcelSheet.Cells[StarRow + j, 9] = MyMeterialQuery.结存金额;
                                     myExcelSheet.Cells[StarRow + j, 10] = MyMeterialQuery.数量;
                                     myExcelSheet.Cells[StarRow + j, 11] = MyMeterialQuery.金额;
                                     myExcelSheet.Cells[StarRow + j, 12] = MyMeterialQuery.损失数量;
                                     myExcelSheet.Cells[StarRow + j, 13] = MyMeterialQuery.损失金额;


                                     ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
                                         "=if(iserror(" + "J" + (StarRow + j) + "/d4),0," + "(J" + (StarRow + j) + "/D4))";
                                     ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                                         "=if(iserror(" + "K" + (StarRow + j) + "/d4),0," + "(K" + (StarRow + j) + "/D4))";

                                     j++;



                                 }
                                 #endregion

                             }
                             #endregion


                         }
                         else  //不需要进行产品还原时
                         {

                             #region 填充材料列表
                             int StarRow = 7;

                             int InsertRowNum = MyMeterialQuerys.Count();


                             Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[8, Missing];
                             for (int i = 0; i < InsertRowNum; i++)
                             {
                                 myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                                 myRange.RowHeight = 11;

                             }
                             myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                             myRange.RowHeight = 11;
                             myRange = null;



                             foreach (var MyMeterialQuery in MyMeterialQuerys)
                             {


                                 myExcelSheet.Cells[StarRow + j, 1] = MyMeterialQuery.材料ID;
                                 myExcelSheet.Cells[StarRow + j, 2] = MyMeterialQuery.材料名称;

                                 myExcelSheet.Cells[StarRow + j, 3] = MyMeterialQuery.计量单位;
                                 myExcelSheet.Cells[StarRow + j, 4] = MyMeterialQuery.期初数量;
                                 myExcelSheet.Cells[StarRow + j, 5] = MyMeterialQuery.期初金额;
                                 myExcelSheet.Cells[StarRow + j, 6] = MyMeterialQuery.投入数量;
                                 myExcelSheet.Cells[StarRow + j, 7] = MyMeterialQuery.投入金额;
                                 myExcelSheet.Cells[StarRow + j, 8] = MyMeterialQuery.结存数量;
                                 myExcelSheet.Cells[StarRow + j, 9] = MyMeterialQuery.结存金额;
                                 myExcelSheet.Cells[StarRow + j, 10] = MyMeterialQuery.数量;
                                 myExcelSheet.Cells[StarRow + j, 11] = MyMeterialQuery.金额;
                                 myExcelSheet.Cells[StarRow + j, 12] = MyMeterialQuery.损失数量;
                                 myExcelSheet.Cells[StarRow + j, 13] = MyMeterialQuery.损失金额;


                                 ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
                                     "=if(iserror(" + "J" + (StarRow + j) + "/d4),0," + "(J" + (StarRow + j) + "/D4))";
                                 ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                                     "=if(iserror(" + "K" + (StarRow + j) + "/d4),0," + "(K" + (StarRow + j) + "/D4))";

                                 j++;



                             }
                             #endregion
                         
                         }

                     }

                 }
                 else{
                 
                     var MyMeterialQuerys = from a in db.TABC_Report_ProductMeterialsCost_WithRealInventory
                                            where a.产品ID == MyProductId
                                            && a.会计期间ID == MyAp
                                            orderby a.材料ID
                                            select a;

                     if (MyMeterialQuerys.Count() == 0)
                     {
                         //没有记录不做处理
                         MeterialFlag = 0;

                     }
                     else
                     {

                         //插入表头信息

                         var ProductInfo = MyMeterialQuerys.Take(1).Single();

                         myExcelSheet.Cells[3, 1] = "产品账户编码：(" + ProductInfo.产品ID.ToString() + ")";
                         myExcelSheet.Cells[4, 1] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                         myExcelSheet.Cells[3, 3] = "会计期间：(" + ProductInfo.会计期间ID.ToString() + ")";
                         myExcelSheet.Cells[4, 4] = ProductInfo.产品数量;
                         myExcelSheet.Cells[3, 7] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                         myExcelSheet.Cells[3, 11] = "产品产量：(" + ProductInfo.产品数量.ToString() + ")";



                         if (ConvertProductInput == true)  //当需要进行产品还原时
                         {
                             #region 当需要进行产品还原
                             var ConvertProductId = from a in MyMeterialQuerys
                                                    join b in db.TABC_JS_Product on a.材料ID equals b.产品ID
                                                    join c in db.TABC_Report_ProductMeterialsCost on b.产品账户ID equals c.产品ID
                                                    where c.会计期间ID == MyAp
                                                    select new
                                                    {
                                                        投料产品ID = a.材料ID,
                                                        成本账户ID = b.产品账户ID,
                                                        产品投料数量 = (a.投入数量 == null ? 0 : a.投入数量),
                                                        投料产品产量 = (c.产品数量 == null ? 0 : c.产品数量)

                                                    };

                             if (ConvertProductId.Count() > 0)
                             {
                                 var MyConvertInfo = ConvertProductId.Take(1).Single();

                                 ConvertMeterialID = MyConvertInfo.投料产品ID;
                                 ConvertProductID = MyConvertInfo.成本账户ID;
                                 if (MyConvertInfo.投料产品产量 > 0)
                                 {

                                     ConvertIndex = (double)MyConvertInfo.产品投料数量 / (double)MyConvertInfo.投料产品产量;
                                 }


                                 var NewMyMeterialQuerys = ((from a in db.TABC_Report_ProductMeterialsCost_WithRealInventory
                                                             where a.产品ID == MyProductId
                                                             && a.会计期间ID == MyAp && a.材料ID != ConvertMeterialID
                                                             orderby a.材料ID
                                                             select new
                                                             {
                                                                 材料ID = a.材料ID,
                                                                 材料名称 = a.材料名称,
                                                                 产品ID = a.产品ID,
                                                                 产品名称 = a.产品名称,
                                                                 产品数量 = a.产品数量,
                                                                 单价 = a.单价,
                                                                 会计期间ID = a.会计期间ID,
                                                                 计量单位 = a.计量单位,
                                                                 结存金额 = a.结存金额,
                                                                 结存数量 = a.结存数量,
                                                                 金额 = a.金额,
                                                                 期初金额 = a.期初金额,
                                                                 期初数量 = a.期初数量,
                                                                 数量 = a.数量,
                                                                 损失金额 = a.损失金额,
                                                                 损失数量 = a.损失数量,
                                                                 投入金额 = a.投入金额,
                                                                 投入数量 = a.投入数量,


                                                             }).Concat(from b in db.TABC_Report_ProductMeterialsCost
                                                                       where b.产品ID == ConvertProductID
                                                                       && b.会计期间ID == MyAp
                                                                       orderby b.材料ID
                                                                       select new
                                                                       {
                                                                           材料ID = b.材料ID,
                                                                           材料名称 = b.材料名称,
                                                                           产品ID = b.产品ID,
                                                                           产品名称 = b.产品名称,
                                                                           产品数量 = (decimal)b.产品数量,
                                                                           单价 = (double)b.单价,
                                                                           会计期间ID = b.会计期间ID,
                                                                           计量单位 = b.计量单位,
                                                                           结存金额 = b.结存金额 - b.结存金额,
                                                                           结存数量 = b.结存数量 - b.结存数量,
                                                                           金额 = (decimal)b.金额 * Convert.ToDecimal(ConvertIndex),
                                                                           期初金额 = b.期初金额 - b.期初金额,
                                                                           期初数量 = b.期初数量 - b.期初数量,
                                                                           数量 = (double)b.数量 * ConvertIndex,
                                                                           损失金额 = b.损失金额 - b.损失金额,
                                                                           损失数量 = b.损失数量 - b.损失数量,
                                                                           投入金额 = Convert.ToDecimal(ConvertIndex) * b.金额,
                                                                           投入数量 =  ConvertIndex*b.数量 


                                                                       })).ToList();

                                 var MyMeterialQuerys_Convert = from c in NewMyMeterialQuerys
                                                                group c by c.材料ID into s
                                                                select new
                                                                {
                                                                    材料ID = s.Key,
                                                                    材料名称 = s.Max(p => p.材料名称),
                                                                    产品ID = s.Max(p => p.产品ID),
                                                                    产品名称 = s.Max(p => p.产品名称),
                                                                    产品数量 = s.Max(p => p.产品数量),
                                                                    单价 = s.Max(p => p.单价),
                                                                    会计期间ID = s.Max(p => p.会计期间ID),
                                                                    计量单位 = s.Max(p => p.计量单位),
                                                                    结存金额 = s.Sum(p => p.结存金额),
                                                                    结存数量 = s.Sum(p => p.结存数量),
                                                                    金额 = s.Sum(p => p.金额),
                                                                    期初金额 = s.Sum(p => p.期初金额),
                                                                    期初数量 = s.Sum(p => p.期初数量),
                                                                    数量 = s.Sum(p => p.数量),
                                                                    损失金额 = s.Sum(p => p.损失金额),
                                                                    损失数量 = s.Sum(p => p.损失数量),
                                                                    投入金额 = s.Sum(p => p.投入金额),
                                                                    投入数量 = s.Sum(p => p.投入数量)



                                                                };

                                 #region 填充材料列表
                                 int StarRow = 7;

                                 int InsertRowNum = MyMeterialQuerys_Convert.Count();


                                 Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[8, Missing];
                                 for (int i = 0; i < InsertRowNum; i++)
                                 {
                                     myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                                     myRange.RowHeight = 11;

                                 }
                                 myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                                 myRange.RowHeight = 11;
                                 myRange = null;



                                 foreach (var MyMeterialQuery in MyMeterialQuerys_Convert)
                                 {


                                     myExcelSheet.Cells[StarRow + j, 1] = MyMeterialQuery.材料ID;
                                     myExcelSheet.Cells[StarRow + j, 2] = MyMeterialQuery.材料名称;

                                     myExcelSheet.Cells[StarRow + j, 3] = MyMeterialQuery.计量单位;
                                     myExcelSheet.Cells[StarRow + j, 4] = MyMeterialQuery.期初数量;
                                     myExcelSheet.Cells[StarRow + j, 5] = MyMeterialQuery.期初金额;
                                     myExcelSheet.Cells[StarRow + j, 6] = MyMeterialQuery.投入数量;
                                     myExcelSheet.Cells[StarRow + j, 7] = MyMeterialQuery.投入金额;
                                     myExcelSheet.Cells[StarRow + j, 8] = MyMeterialQuery.结存数量;
                                     myExcelSheet.Cells[StarRow + j, 9] = MyMeterialQuery.结存金额;
                                     myExcelSheet.Cells[StarRow + j, 10] = MyMeterialQuery.数量;
                                     myExcelSheet.Cells[StarRow + j, 11] = MyMeterialQuery.金额;
                                     myExcelSheet.Cells[StarRow + j, 12] = MyMeterialQuery.损失数量;
                                     myExcelSheet.Cells[StarRow + j, 13] = MyMeterialQuery.损失金额;


                                     ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
                                         "=if(iserror(" + "J" + (StarRow + j) + "/d4),0," + "(J" + (StarRow + j) + "/D4))";
                                     ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                                         "=if(iserror(" + "K" + (StarRow + j) + "/d4),0," + "(K" + (StarRow + j) + "/D4))";

                                     j++;



                                 }
                                 #endregion

                             }
                             #endregion


                         }
                         else  //不需要进行产品还原时
                         {

                             #region 填充材料列表
                             int StarRow = 7;

                             int InsertRowNum = MyMeterialQuerys.Count();


                             Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[8, Missing];
                             for (int i = 0; i < InsertRowNum; i++)
                             {
                                 myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                                 myRange.RowHeight = 11;

                             }
                             myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                             myRange.RowHeight = 11;
                             myRange = null;



                             foreach (var MyMeterialQuery in MyMeterialQuerys)
                             {


                                 myExcelSheet.Cells[StarRow + j, 1] = MyMeterialQuery.材料ID;
                                 myExcelSheet.Cells[StarRow + j, 2] = MyMeterialQuery.材料名称;

                                 myExcelSheet.Cells[StarRow + j, 3] = MyMeterialQuery.计量单位;
                                 myExcelSheet.Cells[StarRow + j, 4] = MyMeterialQuery.期初数量;
                                 myExcelSheet.Cells[StarRow + j, 5] = MyMeterialQuery.期初金额;
                                 myExcelSheet.Cells[StarRow + j, 6] = MyMeterialQuery.投入数量;
                                 myExcelSheet.Cells[StarRow + j, 7] = MyMeterialQuery.投入金额;
                                 myExcelSheet.Cells[StarRow + j, 8] = MyMeterialQuery.结存数量;
                                 myExcelSheet.Cells[StarRow + j, 9] = MyMeterialQuery.结存金额;
                                 myExcelSheet.Cells[StarRow + j, 10] = MyMeterialQuery.数量;
                                 myExcelSheet.Cells[StarRow + j, 11] = MyMeterialQuery.金额;
                                 myExcelSheet.Cells[StarRow + j, 12] = MyMeterialQuery.损失数量;
                                 myExcelSheet.Cells[StarRow + j, 13] = MyMeterialQuery.损失金额;


                                 ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
                                     "=if(iserror(" + "J" + (StarRow + j) + "/d4),0," + "(J" + (StarRow + j) + "/D4))";
                                 ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                                     "=if(iserror(" + "K" + (StarRow + j) + "/d4),0," + "(K" + (StarRow + j) + "/D4))";

                                 j++;



                             }
                             #endregion

                         }


                      


                     }


                 }

                 //燃料动力部分-数据填充部分

                 var MyActivityCostQuerys = from a in db.TABC_Report_ProductActiveCost
                                            join b in db.TABC_DM_CostItems on a.成本细项Id equals b.成本细项ID
                                            where a.产品ID == MyProductId && a.会计期间ID == MyAp && b.参数描述 == "DL"
                                            orderby a.成本细项Id
                                            select new
                                            {
                                                  a.产品ID,
                                                        a.产品名称,
                                                        a.产品数量,
                                                        a.成本细项Id,
                                                        a.成本细项名称,
                                                        a.会计期间ID,
                                                        a.结存金额,
                                                        a.结存数量,
                                                        a.金额,
                                                        a.数量,
                                                        a.期初金额,
                                                        a.期初数量,
                                                        a.损失金额,
                                                        a.损失数量,
                                                        a.投入金额,
                                                        a.投入数量
                                            };

                 if (ConvertProductInput == true)  //当需要进行产品还原时
                 {
                     #region 当需要进行产品还原

                         var NewActivityCostQuerys = (from a in db.TABC_Report_ProductActiveCost
                                                      join b in db.TABC_DM_CostItems on a.成本细项Id equals b.成本细项ID
                                                     where a.产品ID == MyProductId
                                                     && a.会计期间ID == MyAp && b.参数描述 == "DL"
                                                     orderby a.成本细项Id
                                                     select new
                                                     {
                                                        a.产品ID,
                                                        a.产品名称,
                                                        a.产品数量,
                                                        a.成本细项Id,
                                                        a.成本细项名称,
                                                        a.会计期间ID,
                                                        a.结存金额,
                                                        a.结存数量,
                                                        a.金额,
                                                        a.数量,
                                                        a.期初金额,
                                                        a.期初数量,
                                                        a.损失金额,
                                                        a.损失数量,
                                                        a.投入金额,
                                                        a.投入数量


                                                     }).Concat(from d in db.TABC_Report_ProductActiveCost
                                                                 join e in db.TABC_DM_CostItems on d.成本细项Id equals e.成本细项ID
                                                               where d.产品ID == ConvertProductID
                                                               && d.会计期间ID == MyAp && e.参数描述 == "DL"
                                                              orderby d.成本细项Id
                                                               select new
                                                               {

                                                        d.产品ID,
                                                        d.产品名称,
                                                        d.产品数量,
                                                        d.成本细项Id,
                                                        d.成本细项名称,
                                                        d.会计期间ID,
                                                        d.结存金额,
                                                        d.结存数量,
                                                        金额 =d.金额 * Convert.ToDecimal(ConvertIndex),
                                                        数量 = d.数量 * Convert.ToDecimal(ConvertIndex),
                                                        d.期初金额,
                                                        d.期初数量,
                                                        d.损失金额,
                                                        d.损失数量,
                                                        投入金额 = Convert.ToDecimal(ConvertIndex)*d.金额,
                                                        投入数量 = Convert.ToDecimal(ConvertIndex)*d.数量
                                                                                  });

                         var MyActivityCos_Convert = from c in NewActivityCostQuerys
                                                        group c by c.成本细项Id into s
                                                        select new
                                                        {
                                                            成本细项Id = s.Key,
                                                            产品ID = s.Max(p => p.产品ID),
                                                            成本细项名称 = s.Max(p =>p.成本细项名称),
                                                            产品名称 = s.Max(p => p.产品名称),
                                                            产品数量 = s.Max(p => p.产品数量),
                                                            会计期间ID = s.Max(p => p.会计期间ID),
                                                            结存金额 = s.Sum(p => p.结存金额),
                                                            结存数量 = s.Sum(p => p.结存数量),
                                                            金额 = s.Sum(p => p.金额),
                                                             数量 = s.Sum(p => p.数量),
                                                            期初金额 = s.Sum(p => p.期初金额),
                                                            期初数量 = s.Sum(p => p.期初数量),
                                                            损失金额 = s.Sum(p => p.损失金额),
                                                            损失数量 = s.Sum(p => p.损失数量),
                                                            投入金额 = s.Sum(p => p.投入金额),
                                                            投入数量 = s.Sum(p => p.投入数量)



                                                        };


                      // string MyPriorAp = PriorAccountPeriod(MyAp);

                 if (MyActivityCos_Convert.Count() == 0)
                 {
                     //没有记录不做处理
                     ActivityFlag = 0;
                 }
                 else
                 {
                     int StarRow = 11 + j;
                     //插入表头信息
                     int InsertRowNum = MyActivityCos_Convert.Count();
                     Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow + 1, Missing];

                     for (int i = 0; i < InsertRowNum; i++)
                     {
                         range1.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                         //range1.RowHeight = 11;
                     }
                     range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];
                     range1.RowHeight = 11;
                     range1 = null;

                     var ProductInfo = MyActivityCostQuerys.Take(1).Single();


                     myExcelSheet.Cells[3, 1] = "产品账户编码：(" + ProductInfo.产品ID.ToString() + ")";
                     myExcelSheet.Cells[4, 1] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                     myExcelSheet.Cells[3, 3] = "会计期间：(" + ProductInfo.会计期间ID.ToString() + ")";
                     myExcelSheet.Cells[4, 4] = ProductInfo.产品数量;
                     myExcelSheet.Cells[3, 7] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                     myExcelSheet.Cells[3, 11] = "产品产量：(" + ProductInfo.产品数量.ToString() + ")";
                     foreach (var MyActivityCostRow in MyActivityCos_Convert)
                     {
                         string QueryDetailName = MyActivityCostRow.成本细项名称;
                         myExcelSheet.Cells[StarRow + k, 2] = QueryDetailName;
                         myExcelSheet.Cells[StarRow + k, 5] = MyActivityCostRow.期初金额;
                         myExcelSheet.Cells[StarRow + k, 7] = MyActivityCostRow.投入金额;
                         myExcelSheet.Cells[StarRow + k, 9] = MyActivityCostRow.结存金额;
                         myExcelSheet.Cells[StarRow + k, 11] = MyActivityCostRow.金额;
                         myExcelSheet.Cells[StarRow + k, 13] = MyActivityCostRow.损失金额;      
                     myExcelSheet.Cells[StarRow + k, 4] = MyActivityCostRow.期初数量;
                         myExcelSheet.Cells[StarRow + k, 6] = MyActivityCostRow.投入数量;              
                             myExcelSheet.Cells[StarRow, 8] = MyActivityCostRow.结存数量;
                               myExcelSheet.Cells[StarRow, 12] = MyActivityCostRow.损失数量;
                         myExcelSheet.Cells[StarRow, 10] = MyActivityCostRow.数量;
                         
                         int TempRow = StarRow + k;
                         k++;
                     }

                 }


                     #endregion
                 }else
                 {
                          #region 不需要产品还原
                             if (MyActivityCostQuerys.Count() == 0)
                             {
                                 //没有记录不做处理
                                 ActivityFlag = 0;
                             }
                                 else
                                 {
                                     int StarRow = 11 + j;
                                     //插入表头信息
                                     int InsertRowNum = MyActivityCostQuerys.Count();
                                     Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow + 1, Missing];

                                     for (int i = 0; i < InsertRowNum; i++)
                                     {
                                         range1.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                                         //range1.RowHeight = 11;
                                     }
                                     range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];
                                     range1.RowHeight = 11;
                                     range1 = null;

                                     var ProductInfo = MyActivityCostQuerys.Take(1).Single();


                                     myExcelSheet.Cells[3, 1] = "产品账户编码：(" + ProductInfo.产品ID.ToString() + ")";
                                     myExcelSheet.Cells[4, 1] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                                     myExcelSheet.Cells[3, 3] = "会计期间：(" + ProductInfo.会计期间ID.ToString() + ")";
                                     myExcelSheet.Cells[4, 4] = ProductInfo.产品数量;
                                     myExcelSheet.Cells[3, 7] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                                     myExcelSheet.Cells[3, 11] = "产品产量：(" + ProductInfo.产品数量.ToString() + ")";
                                     foreach (var MyActivityCostRow in MyActivityCostQuerys)
                                     {
                                         string QueryDetailName = MyActivityCostRow.成本细项名称;
                                         myExcelSheet.Cells[StarRow + k, 2] = QueryDetailName;
                                         myExcelSheet.Cells[StarRow + k, 5] = MyActivityCostRow.期初金额;
                                         myExcelSheet.Cells[StarRow + k, 7] = MyActivityCostRow.投入金额;
                                         myExcelSheet.Cells[StarRow + k, 9] = MyActivityCostRow.结存金额;
                                         myExcelSheet.Cells[StarRow + k, 11] = MyActivityCostRow.金额;
                                         myExcelSheet.Cells[StarRow + k, 13] = MyActivityCostRow.损失金额;      
                                     myExcelSheet.Cells[StarRow + k, 4] = MyActivityCostRow.期初数量;
                                         myExcelSheet.Cells[StarRow + k, 6] = MyActivityCostRow.投入数量;              
                                             myExcelSheet.Cells[StarRow, 8] = MyActivityCostRow.结存数量;
                                               myExcelSheet.Cells[StarRow, 12] = MyActivityCostRow.损失数量;
                                         myExcelSheet.Cells[StarRow, 10] = MyActivityCostRow.数量;
                                         
                                         int TempRow = StarRow + k;
                                         k++;
                                     }
                #endregion

                                 }
                 }
                    
                //作业费用部分的报表数据填充
                 var MyActivityCostQuerys_ZY = from a in db.TABC_Report_ProductActiveCost
                                               join b in db.TABC_DM_CostItems on a.成本细项Id equals b.成本细项ID
                                               where a.产品ID == MyProductId && a.会计期间ID == MyAp && b.参数描述 == "ZY"
                                               orderby a.成本细项Id
                                               select new
                                               {
                                                    a.产品ID,
                                                        a.产品名称,
                                                        a.产品数量,
                                                        a.成本细项Id,
                                                        a.成本细项名称,
                                                        a.会计期间ID,
                                                        a.结存金额,
                                                        a.结存数量,
                                                        a.金额,
                                                        a.数量,
                                                        a.期初金额,
                                                        a.期初数量,
                                                        a.损失金额,
                                                        a.损失数量,
                                                        a.投入金额,
                                                        a.投入数量
                                               };

                 if (ConvertProductInput == true)  //当需要进行产品还原时
                 {
                     #region 当需要进行产品还原

                     var NewActivityCostQuerys = (from a in db.TABC_Report_ProductActiveCost
                                                  join b in db.TABC_DM_CostItems on a.成本细项Id equals b.成本细项ID
                                                  where a.产品ID == MyProductId
                                                  && a.会计期间ID == MyAp && b.参数描述 == "ZY"
                                                  orderby a.成本细项Id
                                                  select new
                                                  {
                                                      a.产品ID,
                                                      a.产品名称,
                                                      a.产品数量,
                                                      a.成本细项Id,
                                                      a.成本细项名称,
                                                      a.会计期间ID,
                                                      a.结存金额,
                                                      a.结存数量,
                                                      a.金额,
                                                      a.数量,
                                                      a.期初金额,
                                                      a.期初数量,
                                                      a.损失金额,
                                                      a.损失数量,
                                                      a.投入金额,
                                                      a.投入数量


                                                  }).Concat(from d in db.TABC_Report_ProductActiveCost
                                                            join e in db.TABC_DM_CostItems on d.成本细项Id equals e.成本细项ID
                                                            where d.产品ID == ConvertProductID
                                                            && d.会计期间ID == MyAp && e.参数描述 == "ZY"
                                                            orderby d.成本细项Id
                                                            select new
                                                            {

                                                                d.产品ID,
                                                                d.产品名称,
                                                                d.产品数量,
                                                                d.成本细项Id,
                                                                d.成本细项名称,
                                                                d.会计期间ID,
                                                                d.结存金额,
                                                                d.结存数量,
                                                                金额 = d.金额 * Convert.ToDecimal(ConvertIndex),
                                                                数量 = d.数量 * Convert.ToDecimal(ConvertIndex),
                                                                d.期初金额,
                                                                d.期初数量,
                                                                d.损失金额,
                                                                d.损失数量,
                                                                投入金额 = Convert.ToDecimal(ConvertIndex) * d.金额,
                                                                投入数量 = Convert.ToDecimal(ConvertIndex)*d.数量
                                                            });

                     var MyActivityCos_Convert = from c in NewActivityCostQuerys
                                                 group c by c.成本细项Id into s
                                                 select new
                                                 {
                                                     成本细项Id = s.Key,
                                                     产品ID = s.Max(p => p.产品ID),
                                                     成本细项名称 = s.Max(p => p.成本细项名称),
                                                     产品名称 = s.Max(p => p.产品名称),
                                                     产品数量 = s.Max(p => p.产品数量),
                                                     会计期间ID = s.Max(p => p.会计期间ID),
                                                     结存金额 = s.Sum(p => p.结存金额),
                                                     结存数量 = s.Sum(p => p.结存数量),
                                                     金额 = s.Sum(p => p.金额),
                                                     数量 = s.Sum(p => p.数量),
                                                     期初金额 = s.Sum(p => p.期初金额),
                                                     期初数量 = s.Sum(p => p.期初数量),
                                                     损失金额 = s.Sum(p => p.损失金额),
                                                     损失数量 = s.Sum(p => p.损失数量),
                                                     投入金额 = s.Sum(p => p.投入金额),
                                                     投入数量 = s.Sum(p => p.投入数量)



                                                 };


                     // string MyPriorAp = PriorAccountPeriod(MyAp);

                     if (MyActivityCos_Convert.Count() == 0)
                     {
                         //没有记录不做处理
                         ActivityFlag = 0;
                     }
                     else
                     {
                         int StarRow = 16 + j + k;
                         //插入表头信息
                         int InsertRowNum = MyActivityCos_Convert.Count();
                         Microsoft.Office.Interop.Excel.Range range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];

                         for (int i = 0; i < InsertRowNum; i++)
                         {
                             range2.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                             //range1.RowHeight = 11;
                         }
                         range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow + 1, Missing];
                         range2.RowHeight = 11;
                         range2 = null;

                         var ProductInfo = MyActivityCostQuerys_ZY.Take(1).Single();


                         myExcelSheet.Cells[3, 1] = "产品账户编码：(" + ProductInfo.产品ID.ToString() + ")";
                         myExcelSheet.Cells[4, 1] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                         myExcelSheet.Cells[3, 3] = "会计期间：(" + ProductInfo.会计期间ID.ToString() + ")";
                         myExcelSheet.Cells[4, 4] = ProductInfo.产品数量;
                         myExcelSheet.Cells[3, 7] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                         myExcelSheet.Cells[3, 11] = "产品产量：(" + ProductInfo.产品数量.ToString() + ")";

                         StarRow = StarRow - 1;
                         foreach (var MyActivityCostRow_ZY in MyActivityCos_Convert)
                         {

                             string QueryDetailName = MyActivityCostRow_ZY.成本细项名称;
                             myExcelSheet.Cells[StarRow +n , 2] = QueryDetailName;
                             myExcelSheet.Cells[StarRow + n, 5] = MyActivityCostRow_ZY.期初金额;
                             myExcelSheet.Cells[StarRow + n, 7] = MyActivityCostRow_ZY.投入金额;
                             myExcelSheet.Cells[StarRow + n, 9] = MyActivityCostRow_ZY.结存金额;
                             myExcelSheet.Cells[StarRow + n, 11] = MyActivityCostRow_ZY.金额;
                             myExcelSheet.Cells[StarRow + n, 13] = MyActivityCostRow_ZY.损失金额;
                             myExcelSheet.Cells[StarRow + n, 4] = MyActivityCostRow_ZY.期初数量;
                             myExcelSheet.Cells[StarRow + n, 6] = MyActivityCostRow_ZY.投入数量;
                             myExcelSheet.Cells[StarRow+n, 8] = MyActivityCostRow_ZY.结存数量;
                             myExcelSheet.Cells[StarRow+n, 12] = MyActivityCostRow_ZY.损失数量;
                             myExcelSheet.Cells[StarRow+n, 10] = MyActivityCostRow_ZY.数量;

                             n++;



                         }

                     }


                     #endregion
                 }
                 else
                 {
                     #region 不需要产品还原
                     if (MyActivityCostQuerys_ZY.Count() == 0)
                     {
                         //没有记录不做处理
                         ActivityFlag = 0;
                     }
                     else
                     {
                         int StarRow = 16 + j+k;
                         //插入表头信息
                         int InsertRowNum = MyActivityCostQuerys_ZY.Count();
                         Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];

                         for (int i = 0; i < InsertRowNum; i++)
                         {
                             range1.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                             //range1.RowHeight = 11;
                         }
                         range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];
                         range1.RowHeight = 11;
                         range1 = null;

                         var ProductInfo = MyActivityCostQuerys_ZY.Take(1).Single();


                         myExcelSheet.Cells[3, 1] = "产品账户编码：(" + ProductInfo.产品ID.ToString() + ")";
                         myExcelSheet.Cells[4, 1] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                         myExcelSheet.Cells[3, 3] = "会计期间：(" + ProductInfo.会计期间ID.ToString() + ")";
                         myExcelSheet.Cells[4, 4] = ProductInfo.产品数量;
                         myExcelSheet.Cells[3, 7] = "产品账户名称：(" + ProductInfo.产品名称.ToString() + ")";
                         myExcelSheet.Cells[3, 11] = "产品产量：(" + ProductInfo.产品数量.ToString() + ")";

                         StarRow = StarRow - 1;
                         foreach (var MyActivityCostRow in MyActivityCostQuerys_ZY)
                         {
                             string QueryDetailName = MyActivityCostRow.成本细项名称;
                             myExcelSheet.Cells[StarRow + n, 2] = QueryDetailName;
                             myExcelSheet.Cells[StarRow + n, 5] = MyActivityCostRow.期初金额;
                             myExcelSheet.Cells[StarRow + n, 7] = MyActivityCostRow.投入金额;
                             myExcelSheet.Cells[StarRow + n, 9] = MyActivityCostRow.结存金额;
                             myExcelSheet.Cells[StarRow + n, 11] = MyActivityCostRow.金额;
                             myExcelSheet.Cells[StarRow + n, 13] = MyActivityCostRow.损失金额;
                             myExcelSheet.Cells[StarRow + n, 4] = MyActivityCostRow.期初数量;
                             myExcelSheet.Cells[StarRow + n, 6] = MyActivityCostRow.投入数量;
                             myExcelSheet.Cells[StarRow + n, 8] = MyActivityCostRow.结存数量;
                             myExcelSheet.Cells[StarRow + n, 12] = MyActivityCostRow.损失数量;
                             myExcelSheet.Cells[StarRow + n, 10] = MyActivityCostRow.数量;

                             n++;

                         }
                     #endregion

                     }
                 }

                 if (MyActivityCostQuerys.Count() == 0)
                 {
                     //没有记录不做处理
                     ActivityFlag = 0;
                 }



                 if (MeterialFlag + ActivityFlag == 0)
                 {
                     myExcelSheet.Cells[3, 11] = "不存在作业成本";
                 }

                 myExcelBook.Save();


                 Range range = myExcelSheet.get_Range("A1".ToString(), Missing);
                 Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");


                 Range range_Printe = myExcelSheet.get_Range("B1".ToString(), Missing);
                 Hyperlink hyperlink2 = (Hyperlink)range_Printe.Hyperlinks.Add(range_Printe, "/Report/UserReport/" + StrReportName + ".xls", Missing, Missing, "打印");


                 ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                 return StrReportName;

             }catch
             {
                 return "-1";

             }
             finally
             {
                 myExcelBook.Save();
                 myExcelBook.Close(true, StrReportFile, true);
                 System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                 System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                 System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                 ClearThreeObject();
             }


         }
      

         #endregion

        #region 闲置生产能力报表
 

        public string WriteExcel_Report_XZSCLLBB(string MyCenter,string  MyYear, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "闲置生产能力报表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics("闲置生产能力报表", UserName, MyCenter + "-" + MyYear, DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;

          

            try
            {


                ChildCenterList myChildCenterid1 = new ChildCenterList();
                int[] MyDirectChilds = myChildCenterid1.ChildCenterListItems(Convert.ToInt32(MyCenter));
                   
                  var MyCenterLevel =(from a in db.VABC_JS_CostCenterTree
                                      where a.作业中心ID == Convert.ToInt32(MyCenter)
                                      select new 
                                      {
                                          层级 = (int)a.层次+1,
                                          a.作业中心名称
                                      }).Single();


                  var MyCenterLists = from a in db.VABC_JS_CostCenterTree
                                        where (int)a.层次 == MyCenterLevel.层级
                                        && MyDirectChilds.Contains((int)a.作业中心ID)
                                        orderby a.作业中心名称
                                        select a;
    
                
                
               


                if (MyCenterLists.Count() == 0)
                {
                    myExcelSheet.Cells[1, 1] = "没有满足查询的记录";

                }
                else
                {
                    int InsertRowNum = MyCenterLists.Count();


                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[5, Missing];
                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    }
                    myRange = null;

                    int StartRow = 4;
                    int j = 0;
                    myExcelSheet.Cells[2, 1] = "会计年度：" + MyYear + "           部门名称：" + MyCenterLevel.作业中心名称;


                    foreach (var MyCenterlist in MyCenterLists)
                    {


                        ChildCenterList myChildCenterid2 = new ChildCenterList();
                        int[] MyChildCenters = myChildCenterid2.ChildCenterListItems((int)MyCenterlist.作业中心ID);


                         var myQuerys = from a in  db.VABC_Report_ActivityCostAllocation 
                                        where a.会计期间ID.StartsWith(MyYear) && a.成本细项ID == "XZ"
                                        && MyChildCenters.Contains(a.作业中心ID)                                                          
                                        group a by new { a.会计期间ID } into aa
                                        select new
                                        {
                                            aa.Key.会计期间ID,
                                            金额 = aa.Sum(p => p.金额)

                                        };


                         Range range = myExcelSheet.get_Range("A" + (StartRow + j).ToString().ToString(), Missing);
                         Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:location.href='../../view/reportquery/report_xzscllbb_detail.aspx?" + "CenterId=" + MyCenterlist.作业中心ID + "&MyYear=" + MyYear + "&UserName=" + UserName + "';", Missing, Missing, MyCenterlist.作业中心名称.ToString());


                         foreach (var myQuery in myQuerys)
                         {
                             int month = Convert.ToInt32(myQuery.会计期间ID.Substring(4, 2));
                             myExcelSheet.Cells[StartRow + j, month  + 1] = myQuery.金额;


                         }

                         j++;

                      
                    }

                    Range MyRange = myExcelSheet.get_Range(("A"+(InsertRowNum+7).ToString()).ToString(), Missing);
                    Hyperlink hyperlink1 = (Hyperlink)MyRange.Hyperlinks.Add(MyRange, @"javascript:history.back();", Missing, Missing, "返回");


                }



                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion

        #region 成本投入报表-实际价格-各层级
        public string WriteExcel_Report_CBTR_SJJG(int CenterId,string CenterName,string MyYear, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "实际成本投入");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics("实际成本投入", UserName, CenterId.ToString() + "-" + CenterName + "-" + MyYear, DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {

                ChildCenterList myChildCenterid = new ChildCenterList();

                int[] MyCenterList = myChildCenterid.ChildCenterListItems(CenterId);



                //材料
                var myQuery1s = from a in db.VABC_Report_MeterialsCostAllocation
                                where a.会计期间ID.StartsWith(MyYear)
                                &&  MyCenterList.Contains((int)a.作业中心ID)
                                group a by new { a.会计期间ID } into aa
                                select new
                                {
                                    key = aa.Key,
                                    aa.Key.会计期间ID,
                                    金额 = aa.Sum(p => p.金额)

                                };

                foreach (var myQuery1 in myQuery1s)
                {
                    int month = Convert.ToInt32(myQuery1.会计期间ID.Substring(4, 2));
                    myExcelSheet.Cells[6, month*2 + 1] = myQuery1.金额;
                }

                string[] hello = new string[] { "DF", "KY", "LD", "SF", "ZQ", "CL", "RG", "SB", "CF" };
                //燃动费用
                var MyQuery3s = from a in db.VABC_Report_ActivityCostAllocation
                                where a.会计期间ID.StartsWith(MyYear)
                                && new string[] { "DF", "KY", "LD", "SF", "ZQ" }.Contains(a.成本细项ID)
                                 && MyCenterList.Contains(a.作业中心ID)
                                group a by new { a.会计期间ID } into aa
                                select new
                                {

                                    aa.Key.会计期间ID,
                                    金额 = aa.Sum(p => p.金额)

                                };
                foreach (var myQuery3 in MyQuery3s)
                {
                    int month = Convert.ToInt32(myQuery3.会计期间ID.Substring(4, 2));
                    myExcelSheet.Cells[7, month*2 + 1] = myQuery3.金额;
                }

                //人工费用
                var MyQuery4s = from a in db.VABC_Report_ActivityCostAllocation
                                where a.会计期间ID.StartsWith(MyYear)
                               && new string[] { "RG" }.Contains(a.成本细项ID)
                                && MyCenterList.Contains(a.作业中心ID)
                                group a by new { a.会计期间ID } into aa
                                select new
                                {

                                    aa.Key.会计期间ID,
                                    金额 = aa.Sum(p => p.金额)

                                };
                foreach (var myQuery4 in MyQuery4s)
                {
                    int month = Convert.ToInt32(myQuery4.会计期间ID.Substring(4, 2));
                    myExcelSheet.Cells[8, month*2 + 1] = myQuery4.金额;
                }

                //折旧费用
                var MyQuery5s = from a in db.VABC_Report_ActivityCostAllocation
                                where a.会计期间ID.StartsWith(MyYear)
                               && new string[] { "SB", "CF" }.Contains(a.成本细项ID)
                                && MyCenterList.Contains(a.作业中心ID)
                                group a by new { a.会计期间ID,a.成本细项ID } into aa
                                select new
                                {

                                    aa.Key.会计期间ID,
                                    aa.Key.成本细项ID,
                                    金额 = aa.Sum(p => p.金额)

                                };
                foreach (var myQuery5 in MyQuery5s)
                {
                    int month = Convert.ToInt32(myQuery5.会计期间ID.Substring(4, 2));
                    myExcelSheet.Cells[9, month*2 + 1] = myQuery5.金额;
                }
                var MyQuery6s = from a in db.VABC_Report_ActivityCostAllocation
                                where a.会计期间ID.StartsWith(MyYear)
                               && !hello.Contains(a.成本细项ID)
                                && MyCenterList.Contains(a.作业中心ID)
                                group a by new { a.会计期间ID,a.成本细项ID } into aa
                                select new
                                {

                                    aa.Key.会计期间ID,
                                    aa.Key.成本细项ID,
                                    金额 = aa.Sum(p => p.金额)

                                };
                string[] GetDetail = (from a in MyQuery6s select a.成本细项ID).Distinct().ToArray();
                int InsertRowNum = GetDetail.Count();
                Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[11, Missing];

                for (int i = 0; i < InsertRowNum-1; i++)
                {
                    myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    
                }
                myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[10, Missing];
                myRange.RowHeight = 11;
                myRange = null;
                
                for (int i = 0; i < InsertRowNum; i++)
                {
                    var Details=from a in  MyQuery6s where a.成本细项ID==GetDetail[i] select a;
                    foreach (var Detail in Details)
                    {
                        int month = Convert.ToInt32(Detail.会计期间ID.Substring(4, 2));
                        myExcelSheet.Cells[10+i, month * 2 + 1] = Detail.金额;
                        //((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[10+i, month * 2 + 2]).Formula=
                    }

                    myExcelSheet.Cells[10 + i, 2] = (from a in db.TABC_DM_CostItems where a.成本细项ID == GetDetail[i] select a.成本细项名称).Single().ToString(); ;
                }
                
                
                //int allmoney=Convert.ToInt32(((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[10+InsertRowNum+3,3]).Value2);
                for (int i = 0; i < InsertRowNum; i++)
                {
                    var Details = from a in MyQuery6s where a.成本细项ID == GetDetail[i] select a;
                    foreach (var Detail in Details)
                    {
                        int month = Convert.ToInt32(Detail.会计期间ID.Substring(4, 2));
                        Decimal allmoney = Convert.ToDecimal(((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[10 + InsertRowNum + 3,month * 2 + 1]).Value2);
                        if (Convert.ToInt32(allmoney) == 0)
                        {
                            myExcelSheet.Cells[10 + i, month * 2 + 2]="0.00%";
                        }
                        else
                        {
                            myExcelSheet.Cells[10 + i, month * 2 + 2] = Detail.金额 / allmoney;
                        }
                    }

                    //myExcelSheet.Cells[10 + i, 2] = from a in db.TABC_DM_CostItems where a.成本细项ID == GetDetail[i] select a.成本细项名称;
                }
                

                myExcelSheet.Cells[2, 1] = "会计年度：" + MyYear;
                myExcelSheet.Cells[3, 1] = "部门名称："+ "(" + CenterId + ")" + CenterName;
                Range range = myExcelSheet.get_Range("A20".ToString(), Missing);
                Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");






                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch (Exception ee)
            {
                return ee.Message.ToString(); ;

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion

        #region//产品成本汇总表        
        public string WriteExcel_Report_CPCBHZB(string ProductAccount,string Query_Year, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "产品成本汇总表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics("产品成本汇总表", UserName, ProductAccount + "-" + Query_Year , DateTime.Now);
            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {
                var MyProducts = from c in db.VABC_Report_Product
                                where c.产品ID == ProductAccount 
                                  && c.会计期间ID.StartsWith(Query_Year)
                                select c;



                
                foreach (var MyProduct in MyProducts)
                {
                    myExcelSheet.Cells[3, 2] = "("+MyProduct.产品ID+")-" + MyProduct.产品名称;
                    myExcelSheet.Cells[3, 5] = "("+Query_Year + "年)";
                    int month = Convert.ToInt32(MyProduct.会计期间ID.Substring(4, 2));
                    myExcelSheet.Cells[6, month*2 + 4] = MyProduct.转出数量;
                                                             
                }


                var MeterialRowCount = (from a in db.VABC_Report_ProductMeterialsCost
                                         where a.产品ID == ProductAccount
                                         && a.会计期间ID.StartsWith(Query_Year)  orderby a.材料ID
                                         select a.材料ID ).Distinct();

                if (MeterialRowCount.Count() != 0)
                {

                    int InsertRowNum = MeterialRowCount.Count();


                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[9, Missing];
                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    }
                    myRange = null;
                
                }

    
                int StarRow = 8;
                int j = 0;

                var MeterialIDRows = (from a in db.VABC_Report_ProductMeterialsCost
                                        where a.产品ID == ProductAccount
                                        && a.会计期间ID.StartsWith(Query_Year)
                                        orderby a.材料ID
                                        select a.材料ID).Distinct();

                foreach (var MyMeterialID in MeterialIDRows)
                {
                    var MyMeterialRows = from m 
                                        in db.VABC_Report_ProductMeterialsCost
                                         where m.产品ID == ProductAccount
                                         && m.会计期间ID.StartsWith(Query_Year)
                                        && m.材料ID == MyMeterialID  orderby m.会计期间ID
                                        select m;
                 
                    foreach (var MyMeterialRow in MyMeterialRows)
                    {
                        myExcelSheet.Cells[StarRow + j, 1] = MyMeterialRow.材料ID;
                        myExcelSheet.Cells[StarRow + j, 2] = MyMeterialRow.材料名称;
                        myExcelSheet.Cells[StarRow + j, 3] = MyMeterialRow.计量单位;

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 5]).Formula =
                            "=G" + (StarRow + j) +"+"+
                            "I" + (StarRow + j) +"+"+
                                "K" + (StarRow + j)+"+" + "M" + (StarRow + j)+"+" + "O" + (StarRow + j)+"+" + "Q" + (StarRow + j)+"+"
                                + "S" + (StarRow + j)+"+" + "U" + (StarRow + j)+"+" + "W" + (StarRow + j) +"+"+ "Y" + (StarRow + j)+"+"
                                + "AA" + (StarRow + j)+"+" + "AC" + (StarRow + j);

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 4]).Formula =
                           "=F" + (StarRow + j) + "+" +
                           "H" + (StarRow + j) + "+" +
                               "J" + (StarRow + j) + "+" + "L" + (StarRow + j) + "+" + "N" + (StarRow + j) + "+" + "P" + (StarRow + j) + "+"
                               + "R" + (StarRow + j) + "+" + "T" + (StarRow + j) + "+" + "V" + (StarRow + j) + "+" + "X" + (StarRow + j) + "+"
                               + "Z" + (StarRow + j) + "+" + "AB" + (StarRow + j);

                        int month = Convert.ToInt32(MyMeterialRow.会计期间ID.Substring(4, 2));

                        myExcelSheet.Cells[StarRow + j, month*2 + 4] = MyMeterialRow.数量;
                        myExcelSheet.Cells[StarRow + j, month*2 + 5] = MyMeterialRow.金额;


                         
                    }
                    j++; 
                                                                          
                }

                int FeeStar = MeterialRowCount.Count() + 11;

                var MyProduct_ActivityCost = from b in db.VABC_Report_ProductActiveCost
                                             where b.产品ID == ProductAccount
                                             && b.会计期间ID.StartsWith(Query_Year)
                                             select b;
                string[] insert_dl = (from a in db.VABC_Report_ProductActiveCost join b in db.TABC_DM_CostItems on a.成本细项ID equals b.成本细项ID where a.产品ID == ProductAccount && a.会计期间ID.StartsWith(Query_Year) && b.参数描述 == "DL" select a.成本细项ID).Distinct().ToArray();
                int insert_num=insert_dl.Count();
                Microsoft.Office.Interop.Excel.Range myRange1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[FeeStar+1, Missing];

                for (int i = 0; i < insert_num; i++)
                {
                    myRange1.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    //myRange.RowHeight = 11;

                }
                myRange1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[FeeStar, Missing];
                myRange1.RowHeight = 11;
                myRange1 = null;
                for (int i = 0; i < insert_num; i++)
                {
                    var ACs = from df in MyProduct_ActivityCost
                              where df.成本细项ID == insert_dl[i]
                              select df;
                    foreach (var AC in ACs)
                    {
                        int month = Convert.ToInt32(AC.会计期间ID.Substring(4, 2));
                        string name = (from a in db.TABC_DM_CostItems where a.成本细项ID == insert_dl[i] select a.成本细项名称).First().ToString();
                        myExcelSheet.Cells[FeeStar + i, 2] = name;
                        myExcelSheet.Cells[FeeStar+i, month * 2 + 5] = AC.金额;
                        //myExcelSheet.Cells[FeeStar+i, month * 2 + 4] = AC.金额 / UnitPrice(insert_dl[i], AC.会计期间ID);
                        myExcelSheet.Cells[FeeStar + i, month * 2 + 4] = AC.数量;
                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[FeeStar + i, 5]).Formula =

                            "=G" + (FeeStar + i) + "+" +

                            "I" + (FeeStar + i) + "+" +

                            "K" + (FeeStar +i) + "+" + "M" + (FeeStar + i) + "+" + "O" + (FeeStar + i) + "+" + "Q" + (FeeStar + i) + "+"

                            + "S" + (FeeStar + i) + "+" + "U" + (FeeStar + i) + "+" + "W" + (FeeStar + i) + "+" + "Y" + (FeeStar + i) + "+"

                            + "AA" + (FeeStar + i) + "+" + "AC" + (FeeStar + i);

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[FeeStar+i, 4]).Formula =
                           "=F" + (FeeStar + i) + "+" +
                           "H" + (FeeStar + i) + "+" +
                               "J" + (FeeStar + i) + "+" + "L" + (FeeStar + i) + "+" + "N" + (FeeStar + i) + "+" + "P" + (FeeStar + i) + "+"
                               + "R" + (FeeStar + i) + "+" + "T" + (FeeStar + i) + "+" + "V" + (FeeStar + i) + "+" + "X" + (FeeStar + i) + "+"
                               + "Z" + (FeeStar + i) + "+" + "AB" + (FeeStar + i);

                    }
                }
                
                
                FeeStar = FeeStar + insert_num+3;
                string[] insert_zy = (from a in db.VABC_Report_ProductActiveCost join b in db.TABC_DM_CostItems on a.成本细项ID equals b.成本细项ID where a.产品ID == ProductAccount && a.会计期间ID.StartsWith(Query_Year) && b.参数描述 == "ZY" select a.成本细项ID).Distinct().ToArray();
                int insert_num_2 = insert_zy.Count();
                Microsoft.Office.Interop.Excel.Range myRange2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[FeeStar + 1, Missing];

                for (int i = 0; i < insert_num_2; i++)
                {
                    myRange2.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    //myRange.RowHeight = 11;

                }
                myRange2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[FeeStar, Missing];
                myRange2.RowHeight = 11;
                myRange2 = null;
                for (int i = 0; i < insert_num_2; i++)
                {
                    var ACs = from df in MyProduct_ActivityCost
                              where df.成本细项ID == insert_zy[i]
                              select df;
                    foreach (var AC in ACs)
                    {
                        int month = Convert.ToInt32(AC.会计期间ID.Substring(4, 2));
                        string name = (from a in db.TABC_DM_CostItems where a.成本细项ID == insert_zy[i] select a.成本细项名称).First().ToString();
                        myExcelSheet.Cells[FeeStar + i, 2] = name;
                        myExcelSheet.Cells[FeeStar + i, month * 2 + 5] = AC.金额;
                        myExcelSheet.Cells[FeeStar + i, month * 2 + 4] = AC.数量;
                        //myExcelSheet.Cells[FeeStar + i, month * 2 + 4] = AC.金额 / UnitPrice(insert_zy[i], AC.会计期间ID);

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[FeeStar + i, 5]).Formula =

                            "=G" + (FeeStar + i) + "+" +

                            "I" + (FeeStar + i) + "+" +

                            "K" + (FeeStar + i) + "+" + "M" + (FeeStar + i) + "+" + "O" + (FeeStar + i) + "+" + "Q" + (FeeStar + i) + "+"

                            + "S" + (FeeStar + i) + "+" + "U" + (FeeStar + i) + "+" + "W" + (FeeStar + i) + "+" + "Y" + (FeeStar + i) + "+"

                            + "AA" + (FeeStar + i) + "+" + "AC" + (FeeStar + i);

                        //((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[FeeStar + i, 4]).Formula =
                           //"=F" + (FeeStar + i) + "+" +
                           //"H" + (FeeStar + i) + "+" +
                               //"J" + (FeeStar + i) + "+" + "L" + (FeeStar + i) + "+" + "N" + (FeeStar + i) + "+" + "P" + (FeeStar + i) + "+"
                               //+ "R" + (FeeStar + i) + "+" + "T" + (FeeStar + i) + "+" + "V" + (FeeStar + i) + "+" + "X" + (FeeStar + i) + "+"
                               //+ "Z" + (FeeStar + i) + "+" + "AB" + (FeeStar + i);

                    }
                }
                

                

                Range range = myExcelSheet.get_Range(("A" + (FeeStar + 18).ToString()).ToString(), Missing);
                Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");


                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }
        #endregion

        #region//实物盘存信息表查询

        public string WriteExcel_Report_SWPCXXB(string ProductAccount, string Query_Year, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "实物盘存信息表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics("实物盘存信息表", UserName, ProductAccount + "-" + Query_Year, DateTime.Now);
            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {

                string PriorYear = (Convert.ToInt32(Query_Year) - 1).ToString()+"12";


                var HeadInfo = (from a in db.TABC_JS_Product
                               where a.产品账户ID == ProductAccount
                               select new
                               {
                                   a.产品账户ID,
                                   a.产品账户名称

                               }).Single();

                myExcelSheet.Cells[2, 1] = "产品账户编码：("+HeadInfo.产品账户ID+")";
                myExcelSheet.Cells[2, 4] = "产品名称(" + HeadInfo.产品账户名称 + ")";
                myExcelSheet.Cells[2, 7] = "会计期间(" + Query_Year + ")"; 


                var MeterialRowCounts = (from c in db.VABC_Report_Meterials_Real_Inventory
                                         where c.产品ID == ProductAccount
                                           && (c.会计期间ID.StartsWith(Query_Year) || c.会计期间ID == PriorYear)
                                        select c.材料ID).Distinct();


                //得出原材料的总行数，并插入相同数量的空格。
                if (MeterialRowCounts.Count() != 0)
                {

                    int InsertRowNum = MeterialRowCounts.Count();


                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[6, Missing];
                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    }
                    myRange = null;


                    int StarRow = 5;
                    int j = 0;
                    foreach (var MeterialRowCount in MeterialRowCounts)
                    {
                        var MeterialRows = from c in db.VABC_Report_Meterials_Real_Inventory
                                           where c.产品ID == ProductAccount
                                           && c.材料ID == MeterialRowCount
                                          && (c.会计期间ID.StartsWith(Query_Year) || c.会计期间ID == PriorYear)
                                          orderby  c.会计期间ID
                                           select c;

                        foreach (var MeterialRow in MeterialRows)
                        {
                            myExcelSheet.Cells[StarRow + j, 1] = MeterialRow.材料ID;
                            myExcelSheet.Cells[StarRow + j, 2] = MeterialRow.材料名称;
                            myExcelSheet.Cells[StarRow + j, 3] = MeterialRow.计量单位;
                       
                            if (MeterialRow.会计期间ID == PriorYear)
                            {
                                myExcelSheet.Cells[StarRow + j, 4] = MeterialRow.期末数量;
                                myExcelSheet.Cells[StarRow + j, 5] = MeterialRow.期末金额;

                            }
                            else
                            { 
                            int month = Convert.ToInt32(MeterialRow.会计期间ID.Substring(4, 2));

                            myExcelSheet.Cells[StarRow + j, month * 2 + 4] = MeterialRow.期末数量;
                            myExcelSheet.Cells[StarRow + j, month * 2 + 5] = MeterialRow.期末金额;
                            }
                        }

                        j++;



                    }

                    Range range = myExcelSheet.get_Range(("A"+(j+8).ToString()).ToString(), Missing);
                    Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");


                }


                myExcelBook.Save();


                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }
        #endregion

        #region//产品成本汇总表之单位成本

        public string WriteExcel_Report_CPCBHZB_UNIT(string ProductAccount, string Query_Year, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "产品成本汇总表之单位成本");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];
            //写入报表点击查询页面
            WriteReportStatistics("产品成本汇总表之单位成本", UserName, ProductAccount + "-" + Query_Year, DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {
                var MyProducts = from c in db.VABC_Report_Product
                                 where c.产品ID == ProductAccount
                                   && c.会计期间ID.StartsWith(Query_Year)
                                 select c;




                foreach (var MyProduct in MyProducts)
                {
                    myExcelSheet.Cells[3, 2] = "(" + MyProduct.产品ID + ")-" + MyProduct.产品名称;
                    myExcelSheet.Cells[3, 5] = "(" + Query_Year + "年)";
                    int month = Convert.ToInt32(MyProduct.会计期间ID.Substring(4, 2));
                    myExcelSheet.Cells[6, month * 2 + 4] = MyProduct.转出数量;
                    var MeterialCostSum = (from a in db.VABC_Report_ProductMeterialsCost
                                           where a.产品ID == MyProduct.产品ID
                                           && a.会计期间ID == MyProduct.会计期间ID
                                           select a.金额).Sum();
                    var ActivityCostSum = (from a in db.VABC_Report_ProductActiveCost
                                           where a.产品ID == MyProduct.产品ID
                                           && a.会计期间ID == MyProduct.会计期间ID
                                           select a.金额).Sum();
                    decimal SumCost = (decimal)(MeterialCostSum.ToString() == "" ? 0 : MeterialCostSum)
                                       + (decimal)(ActivityCostSum.ToString() == "" ? 0 : ActivityCostSum);
                    if (SumCost.ToString() != "" && MyProduct.转出数量 != 0)
                    {

                        myExcelSheet.Cells[6, month * 2 + 5] = SumCost / MyProduct.转出数量;
                    }

                }


                var MeterialRowCount = (from a in db.VABC_Report_ProductMeterialsCost
                                        where a.产品ID == ProductAccount
                                        && a.会计期间ID.StartsWith(Query_Year)
                                        orderby a.材料ID
                                        select a.材料ID).Distinct();

                if (MeterialRowCount.Count() != 0)
                {

                    int InsertRowNum = MeterialRowCount.Count();


                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[9, Missing];
                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    }
                    myRange = null;

                }


                int StarRow = 8;
                int j = 0;

                var MeterialIDRows = (from a in db.VABC_Report_ProductMeterialsCost
                                      where a.产品ID == ProductAccount
                                      && a.会计期间ID.StartsWith(Query_Year)
                                      orderby a.材料ID
                                      select a.材料ID).Distinct();

                foreach (var MyMeterialID in MeterialIDRows)
                {
                    var MyMeterialRows = from m
                                        in db.VABC_Report_ProductMeterialsCost
                                         where m.产品ID == ProductAccount
                                         && m.会计期间ID.StartsWith(Query_Year)
                                        && m.材料ID == MyMeterialID
                                         orderby m.会计期间ID
                                         select m;
                    decimal TotalNum = 0;
                    decimal TotalMoney = 0;
                    decimal TotalProduct = 0;
                    foreach (var MyMeterialRow in MyMeterialRows)
                    {
                        myExcelSheet.Cells[StarRow + j, 1] = MyMeterialRow.材料ID;
                        myExcelSheet.Cells[StarRow + j, 2] = MyMeterialRow.材料名称;
                        myExcelSheet.Cells[StarRow + j, 3] = MyMeterialRow.计量单位;



                        int month = Convert.ToInt32(MyMeterialRow.会计期间ID.Substring(4, 2));

                        //myExcelSheet.Cells[StarRow + j, month * 2 + 4] = MyMeterialRow.数量;
                        if (MyMeterialRow.产品数量 != 0)
                        {
                            myExcelSheet.Cells[StarRow + j, month * 2 + 5] = (decimal)MyMeterialRow.金额 / (decimal)MyMeterialRow.产品数量;
                            myExcelSheet.Cells[StarRow + j, month * 2 + 4] = (decimal)MyMeterialRow.数量 / (decimal)MyMeterialRow.产品数量;

                        };
                        TotalNum = TotalNum + (decimal)MyMeterialRow.数量;
                        TotalMoney = TotalMoney + (decimal)MyMeterialRow.金额;
                        TotalProduct = TotalProduct + (decimal)MyMeterialRow.产品数量;


                    }
                    if (TotalProduct != 0)
                    {
                        myExcelSheet.Cells[StarRow + j, 5] = TotalMoney / TotalProduct;
                        myExcelSheet.Cells[StarRow + j, 4] = TotalNum / TotalProduct;
                    }


                    j++;

                }

                int FeeStar = MeterialRowCount.Count() + 11;

                var MyProduct_ActivityCost = from b in db.VABC_Report_ProductActiveCost
                                             where b.产品ID == ProductAccount
                                             && b.会计期间ID.StartsWith(Query_Year)
                                             select b;
                string[] insert_dl = (from a in db.VABC_Report_ProductActiveCost join b in db.TABC_DM_CostItems on a.成本细项ID equals b.成本细项ID where a.产品ID == ProductAccount && a.会计期间ID.StartsWith(Query_Year) && b.参数描述 == "DL" select a.成本细项ID).Distinct().ToArray();
                int insert_num = insert_dl.Count();
                Microsoft.Office.Interop.Excel.Range myRange1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[FeeStar + 1, Missing];

                for (int i = 0; i < insert_num; i++)
                {
                    myRange1.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    //myRange.RowHeight = 11;

                }
                myRange1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[FeeStar, Missing];
                myRange1.RowHeight = 11;
                myRange1 = null;
                decimal ACMoneySum = 0;
                decimal ACMountSum = 0;
                decimal ACProductSum = 0;
                for (int i = 0; i < insert_num; i++)
                {
                    var ACs = from df in MyProduct_ActivityCost
                              where df.成本细项ID == insert_dl[i]
                              select df;
                    foreach (var AC in ACs)
                    {
                        int month = Convert.ToInt32(AC.会计期间ID.Substring(4, 2));
                        string name = (from a in db.TABC_DM_CostItems where a.成本细项ID == insert_dl[i] select a.成本细项名称).First().ToString();
                        myExcelSheet.Cells[FeeStar + i, 2] = name;
                        if (AC.产品数量 != 0)
                        {
                            myExcelSheet.Cells[FeeStar+i, month * 2 + 5] = (decimal)AC.金额 / (decimal)AC.产品数量;

                            myExcelSheet.Cells[FeeStar + i, month * 2 + 4] = (decimal)AC.数量 / (decimal)AC.产品数量;

                            ACMoneySum = ACMoneySum + (decimal)AC.金额;
                            ACMountSum = ACMountSum + (decimal)AC.数量;
                            ACProductSum = ACProductSum + (decimal)AC.产品数量;

                        }
                        if (ACProductSum != 0)
                        {
                            myExcelSheet.Cells[FeeStar + i, 4] = ACMountSum / ACProductSum;

                            myExcelSheet.Cells[FeeStar + i, 5] = ACMoneySum / ACProductSum;
                        }
   
                    }
                }

                ACMoneySum = 0;
                ACMountSum = 0;
                ACProductSum = 0;
                FeeStar = FeeStar + insert_num + 3;
                string[] insert_zy = (from a in db.VABC_Report_ProductActiveCost join b in db.TABC_DM_CostItems on a.成本细项ID equals b.成本细项ID where a.产品ID == ProductAccount && a.会计期间ID.StartsWith(Query_Year) && b.参数描述 == "ZY" select a.成本细项ID).Distinct().ToArray();
                int insert_num_2 = insert_zy.Count();
                Microsoft.Office.Interop.Excel.Range myRange2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[FeeStar + 1, Missing];

                for (int i = 0; i < insert_num_2; i++)
                {
                    myRange2.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    //myRange.RowHeight = 11;

                }
                myRange2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[FeeStar, Missing];
                myRange2.RowHeight = 11;
                myRange2 = null;
                for (int i = 0; i < insert_num_2; i++)
                {
                    var ACs = from df in MyProduct_ActivityCost
                              where df.成本细项ID == insert_zy[i]
                              select df;
                    foreach (var AC in ACs)
                    {
                        int month = Convert.ToInt32(AC.会计期间ID.Substring(4, 2));
                        string name = (from a in db.TABC_DM_CostItems where a.成本细项ID == insert_zy[i] select a.成本细项名称).First().ToString();
                        myExcelSheet.Cells[FeeStar + i, 2] = name;
                        if (AC.产品数量 != 0)
                        {
                            myExcelSheet.Cells[FeeStar + i, month * 2 + 5] = (decimal)AC.金额 / (decimal)AC.产品数量;
                            myExcelSheet.Cells[FeeStar + i, month * 2 + 4] = (decimal)AC.数量 / (decimal)AC.产品数量;
                            //myExcelSheet.Cells[FeeStar + i, month * 2 + 4] = AC.金额 / (UnitPrice(insert_zy[i], AC.会计期间ID) * (decimal)AC.产品数量);
                            ACMountSum = ACMountSum + (decimal)AC.数量;
                            ACMoneySum = ACMoneySum + (decimal)AC.金额;
                            ACProductSum = ACProductSum + (decimal)AC.产品数量;

                        }
                        if (ACProductSum != 0)
                        {
                            myExcelSheet.Cells[FeeStar + i, 5] = ACMoneySum / ACProductSum;
                            myExcelSheet.Cells[FeeStar + i, 4] = ACMountSum / ACProductSum;
                        }

                    }
                }




                Range range = myExcelSheet.get_Range(("A" + (FeeStar + 18).ToString()).ToString(), Missing);
                Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");


                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }
        #endregion

        #region//产品成本台账
        public string WriteExcel_Report_CPCBTZ(string AccountPeriodValue, string UserName,string IsBaseReport)
        {
         
                string[] Myfile = ReturnfilePath(UserName, (IsBaseReport == "yes"?"产品成本台账_Base":"产品成本台账"));
                string StrSamepleFile = Myfile[0];
                string StrReportName = Myfile[1];
                string StrReportFile = Myfile[2];
                string StrReportFileHtml = Myfile[3];

                //写入报表点击查询页面
                WriteReportStatistics((IsBaseReport == "yes" ? "产品成本台账_Base" : "产品成本台账"), UserName, AccountPeriodValue, DateTime.Now);
                ClearThreeObject();

         



            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;

            int StartRow = 4;
            int j = 0;
            int SumRow = 0;
            string FirstThreeNumber = "";
           

            try
            {

                var isValid = db.TABC_Report_ProductInProcessSumCost.AsEnumerable().Where(p => p.会计期间ID == AccountPeriodValue).Sum(
                                           p => p.转出金额
                                           );


                if ((decimal)isValid == 0)
               {
                   return "-1";

               }
               else
               {

                   var myQuerys = from a in db.TABC_Report_ProductInProcessSumCost
                                  where a.会计期间ID == AccountPeriodValue
                             
                                  orderby a.车间, a.产品ID
                                  select new 
                                  {
                                    a.产品ID,
                                    a.产品账户名称,
                                    a.车间,
                                    a.计量单位,
                                    a.结存金额,
                                    a.期初金额,
                                    a.损失金额,
                                    a.投入金额,
                                    a.转出金额,
                                    产品前三位 = a.产品ID.ToString().Substring(0,3)
                                  
                                  };

                   if (myQuerys.Count() == 0)
                   {

                       return "-1";

                   }
                   else
                   {

                       int InsertRowNum = myQuerys.Count();
                       //计算一共有多少行数据行。
       
                       //计算需要插入多少个合计项的行。
                       int TotalRowNum = (from a in myQuerys
                                          select new { a.产品前三位 }).Distinct().Count();
                       //计算一共需要插入的行数。
                       int AllRowNum = InsertRowNum + TotalRowNum;



                       Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[5, Missing];
                       for (int i = 0; i < AllRowNum; i++)
                       {
                           myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                       }
                       myRange = null;


                       myExcelSheet.Cells[2, 1] ="会计期间："+ "("+ AccountPeriodValue +")";

                       foreach (var myQuery in myQuerys)
                       {
                           if ((StartRow + j) == SumRow)
                           {
                               //判断当前行是合计行的时候，向下跳转一行。
                               j++;
                           }
                          
                         //判断当前的分组字段是否改变
                           if(FirstThreeNumber != myQuery.产品前三位)
                           {
                                                       
                               int MyRowCount = (from a in myQuerys
                                              where a.产品前三位 == myQuery.产品前三位
                                              select a).Count();
                           


                            myExcelSheet.Cells[StartRow + j + MyRowCount,1] = "小计：";

                            Microsoft.Office.Interop.Excel.Range myRange1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.get_Range(myExcelSheet.Cells[StartRow + j + MyRowCount , 1], myExcelSheet.Cells[StartRow + j + MyRowCount , 9]);
                            myRange1.Interior.ColorIndex = 19;
                            myRange1 = null;
                           
                            SumRow = StartRow + j + MyRowCount ;

                            FirstThreeNumber = myQuery.产品前三位;

                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount , 5]).Formula =
                                "=" + "sum(E" + (StartRow + j ) + ":" + "E" + (StartRow + j + MyRowCount -1 );
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount , 6]).Formula =
                               "=" + "sum(F" + (StartRow + j) + ":" + "F" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount , 7]).Formula =
                               "=" + "sum(G" + (StartRow + j) + ":" + "G" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount , 8]).Formula =
                               "=" + "sum(H" + (StartRow + j) + ":" + "H" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 9]).Formula =
                         "=" + "sum(I" + (StartRow + j) + ":" + "I" + (StartRow + j + MyRowCount - 1);
                           }


                           myExcelSheet.Cells[StartRow + j, 1] = myQuery.车间;
                           myExcelSheet.Cells[StartRow + j, 2] = myQuery.产品ID;

                           myExcelSheet.Cells[StartRow + j, 3] = myQuery.产品账户名称;
                           if (myQuery.计量单位.ToString() == "")
                           {
                               myExcelSheet.Cells[StartRow + j, 4] = "(小试)";
                           }
                           else
                           {
                               myExcelSheet.Cells[StartRow + j, 4] = "(量产)"+myQuery.计量单位;
                           }
                          
                           myExcelSheet.Cells[StartRow + j, 5] = myQuery.期初金额;
                           myExcelSheet.Cells[StartRow + j, 6] = myQuery.投入金额;
                           myExcelSheet.Cells[StartRow + j, 7] = myQuery.转出金额;
                           myExcelSheet.Cells[StartRow + j, 8] = myQuery.损失金额;
                           myExcelSheet.Cells[StartRow + j, 9] = myQuery.结存金额;

                           j++;

                       }



                       decimal TotalQC = (decimal)(from a in db.TABC_Report_ProductInProcessSumCost
                                      where a.会计期间ID == AccountPeriodValue
                                      select a.期初金额).Sum();
                       decimal TotalTU = (decimal)(from a in db.TABC_Report_ProductInProcessSumCost
                                                   where a.会计期间ID == AccountPeriodValue
                                                   select a.投入金额).Sum();
                       decimal TotalZC = (decimal)(from a in db.TABC_Report_ProductInProcessSumCost
                                                   where a.会计期间ID == AccountPeriodValue
                                                   select a.转出金额).Sum();
                       decimal TotalSS = (decimal)(from a in db.TABC_Report_ProductInProcessSumCost
                                                   where a.会计期间ID == AccountPeriodValue
                                                   select a.损失金额).Sum();
                       decimal TotalJC = (decimal)(from a in db.TABC_Report_ProductInProcessSumCost
                                                   where a.会计期间ID == AccountPeriodValue
                                                   select a.结存金额).Sum();

                       myExcelSheet.Cells[StartRow + j + 3, 5] = TotalQC;
                       myExcelSheet.Cells[StartRow + j + 3, 6] = TotalTU;
                       myExcelSheet.Cells[StartRow + j + 3, 7] = TotalZC;
                       myExcelSheet.Cells[StartRow + j + 3, 8] = TotalSS;
                       myExcelSheet.Cells[StartRow + j + 3, 9] = TotalJC;

                     //  Range range = myExcelSheet.get_Range(("A" + (StartRow + j + 4).ToString()).ToString(), Missing);
                     //  Hyperlink hyperlink1 = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");
                      // Range range_Printe = myExcelSheet.get_Range(("E" + (StartRow + j + 4).ToString()).ToString(), Missing);
                      // Hyperlink hyperlink2 = (Hyperlink)range_Printe.Hyperlinks.Add(range_Printe, "/Report/UserReport/" + StrReportName + ".xls", Missing, Missing, "返回"); 
                   }
                 
               }

              



                myExcelBook.Save();

                Range range = myExcelSheet.get_Range(("A" + (StartRow + j + 4).ToString()).ToString(), Missing);
                Hyperlink hyperlink1 = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");
                Range range_Printe = myExcelSheet.get_Range(("E" + (StartRow + j + 4).ToString()).ToString(), Missing);
                Hyperlink hyperlink2 = (Hyperlink)range_Printe.Hyperlinks.Add(range_Printe, "/Report/UserReport/" + StrReportName + ".xls", Missing, Missing, "打印"); 
            
                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }
        }

        #endregion 


        #region//成本及费用明细表
        public string WriteExcel_Report_CBJFYMXB(string AccountPeriodValue, string UserName)
        {
            string[] Myfile = ReturnfilePath(UserName, "成本及费用明细表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];
            //写入报表点击查询页面
            WriteReportStatistics("成本及费用明细表", UserName, AccountPeriodValue, DateTime.Now);
            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;




            try
            {

                var isValid = db.VABC_Report_ProductInProcessSumCost_分细项.AsEnumerable().Where(p => p.会计期间ID == AccountPeriodValue).Sum(
                                           p => p.转出金额
                                           );


                if ((decimal)isValid == 0)
                {

                    return "-1";

                }
                else
                {
                    //统计非96开头的
                    var myQuerys = from c in
                                       (from a in db.VABC_Report_ProductInProcessSumCost_分细项
                                        where a.会计期间ID == AccountPeriodValue
                                        && !a.产品ID.StartsWith("96")
                                        group a by new { a.成本细项ID, a.会计期间ID } into g
                                        select new
                                        {
                                            g.Key.成本细项ID,
                                            g.Key.会计期间ID,
                                            结存金额 = g.Sum(p => p.结存金额),
                                            期初金额 = g.Sum(p => p.期初金额),
                                            损失金额 = g.Sum(p => p.损失金额),
                                            投入金额 = g.Sum(p => p.投入金额),
                                            转出金额 = g.Sum(p => p.转出金额)

                                        })
                                   join b in db.TABC_DM_CostItems on c.成本细项ID equals b.成本细项ID orderby b.报表顺序码
                                   select new { c.成本细项ID,
                                                c.会计期间ID,
                                                c.结存金额,
                                                c.期初金额,
                                                c.损失金额,
                                                c.投入金额,
                                                c.转出金额,
                                                b.成本细项名称,
                                                b.报表顺序码
                                   };

                    if (myQuerys.Count() == 0)
                    {


                        return "-1";
                    }
                    else
                    {
                        int InsertRowNum = myQuerys.Count();

                        Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[5, Missing];
                        for (int i = 0; i < InsertRowNum; i++)
                        {
                            myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);

                        }
                        // myRange.RowHeight = 19;
                        myRange = null;
                        int StartRow = 4;
                        int j = 0;


                        myExcelSheet.Cells[2, 1] = "会计期间：(" + AccountPeriodValue + ")"; ;

                        foreach (var myQuery in myQuerys)
                        {
                            myExcelSheet.Cells[StartRow + j, 1] = myQuery.成本细项名称;
                            myExcelSheet.Cells[StartRow + j, 2] = myQuery.期初金额;
                            myExcelSheet.Cells[StartRow + j, 3] = myQuery.投入金额;
                            myExcelSheet.Cells[StartRow + j, 4] = myQuery.转出金额;
                            myExcelSheet.Cells[StartRow + j, 5] = myQuery.损失金额;
                            myExcelSheet.Cells[StartRow + j, 6] = myQuery.结存金额;
                            j++;

                        }


                        //统计非96开头的
                        var myQuerys2 = from a in db.VABC_Report_ProductInProcessSumCost_分细项
                                        where a.会计期间ID == AccountPeriodValue
                                        && a.产品ID.StartsWith("96")
                                        group a by new { a.会计期间ID } into g
                                        select new
                                        {

                                            g.Key.会计期间ID,
                                            结存金额 = g.Sum(p => p.结存金额),
                                            期初金额 = g.Sum(p => p.期初金额),
                                            损失金额 = g.Sum(p => p.损失金额),
                                            投入金额 = g.Sum(p => p.投入金额),
                                            转出金额 = g.Sum(p => p.转出金额)

                                        };

                        if (myQuerys2.Count() == 1)
                        {

                            var myQuery2 = myQuerys2.Single();
                            Microsoft.Office.Interop.Excel.Range range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[5+j, Missing];
                            range2.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                            range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[5 + j, Missing];
                            range2.RowHeight = 11;
                            range2 = null;
                            myExcelSheet.Cells[StartRow + j + 4, 2] = myQuery2.期初金额;
                            myExcelSheet.Cells[StartRow + j + 4, 3] = myQuery2.投入金额;
                            myExcelSheet.Cells[StartRow + j + 4, 4] = myQuery2.转出金额;
                            myExcelSheet.Cells[StartRow + j + 4, 5] = myQuery2.损失金额;
                            myExcelSheet.Cells[StartRow + j + 4, 6] = myQuery2.结存金额;

                            

                        }
                        Range range = myExcelSheet.get_Range("A28".ToString(), Missing);
                        Hyperlink hyperlink1 = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回"); 

                    }

                }





                myExcelBook.Save();

                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }
        }

        #endregion 


        #region//费用分配表
        public string WriteExcel_Report_FYFPB(int MyCenterId,string MyCostItem,string AccountPeriodValue, string UserName)
        {
            string[] Myfile = ReturnfilePath(UserName, "费用分配表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];
            //写入报表点击查询页面
            WriteReportStatistics("费用分配表", UserName, MyCenterId.ToString() + "-" + MyCostItem+"-" +AccountPeriodValue, DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;




            try
            {
                ChildCenterList MyChildCenterList = new ChildCenterList();
                int[] MyDirectChilds = MyChildCenterList.ChildCenterListItems(MyCenterId);

                 var MyCenterLevel = (from a in db.VABC_JS_CostCenterTree
                                      where a.作业中心ID == MyCenterId
                                      select new
                                      {
                                          层级 = (int)a.层次 + 1,
                                          a.作业中心名称
                                      }).Single();


                 var MyCenterLists = from a in db.VABC_JS_CostCenterTree
                                     where (int)a.层次 == MyCenterLevel.层级
                                     && MyDirectChilds.Contains((int)a.作业中心ID)
                                     orderby a.作业中心名称
                                     select a;



                 if (MyCenterLists.Count() == 0)
                {
                    


                }
                else
                {
                    int InsertRowNum = MyCenterLists.Count();


                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[5, Missing];
                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    }
                    myRange = null;

                    int StartRow = 4;
                    int j = 0;



                    var CostItemName = (from b in db.TABC_DM_CostItems
                                       where b.成本细项ID == MyCostItem
                                       select new
                                           {
                                               b.成本细项ID,
                                               b.成本细项名称
                                           }).Single();

                    myExcelSheet.Cells[2, 1] = "费用项目名称：(" + CostItemName.成本细项ID + ")" + CostItemName.成本细项名称;

               
                    foreach (var MyCenterList in MyCenterLists)
                    {
                        ChildCenterList MyChildCenterList1 = new ChildCenterList();
                        int[] MyChildCenterLists = MyChildCenterList1.ChildCenterListItems((int)MyCenterList.作业中心ID);


                        var MySum = db.VABC_Report_ActivityCostAllocation.AsEnumerable().Where(p => p.会计期间ID == AccountPeriodValue 
                                                && p.成本细项ID == MyCostItem
                                                  && MyChildCenterLists.Contains(p.作业中心ID) 
                                                  && p.金额 > 0    
                                                ).Sum(p => p.金额);

                        decimal summoney = MySum.GetValueOrDefault(0);
                        
                        //myExcelSheet.Cells[StartRow + j, 1] =MyCenterList.作业中心名称+"("+MyCenterList.作业中心ID+")";


                        Range range1 = myExcelSheet.get_Range("A" + (StartRow + j).ToString().ToString(), Missing);
                        Hyperlink hyperlink = (Hyperlink)range1.Hyperlinks.Add(range1, @"javascript:location.href='../../view/reportquery/report_fyfpb_detail.aspx?" + "CenterId=" + MyCenterList.作业中心ID + "&CostItem=" + MyCostItem + "&MyAp=" + AccountPeriodValue + "&UserName=" + UserName + "';", Missing, Missing, MyCenterList.作业中心名称.ToString());

                        //myExcelSheet.Cells[StartRow + j, 2] = myQuery.产品ID;
                        //myExcelSheet.Cells[StartRow + j, 3] = myQuery.产品名称;
                        //myExcelSheet.Cells[StartRow + j, 4] = myQuery.计量单位;
                        myExcelSheet.Cells[StartRow + j, 5] = summoney;


                     
                        j++;

                    }

             
                    Range range = myExcelSheet.get_Range("A" + (StartRow + j + 4).ToString(), Missing);
                    Hyperlink hyperlink1 = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回"); 
                   // myExcelSheet.Hyperlinks.Add
                }


                myExcelBook.Save();

                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }
        }

        #endregion 

        #region//产品成本统计表
        public string WriteExcel_Report_CPCBTJB(string  ProductId, string MyYear, string UserName)
        {
            string[] Myfile = ReturnfilePath(UserName, "产品成本统计表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];
            //写入报表点击查询页面
            WriteReportStatistics("产品成本统计表", UserName, ProductId + "-" + MyYear, DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;




            try
            {
                var myQuery1s = from a in db.VABC_Report_Product
                                where a.会计期间ID.StartsWith(MyYear)
                                && a.产品ID == ProductId
                                orderby a.会计期间ID
                                select new
                                {
                                    a.产品ID,
                                    a.会计期间ID,
                                    a.产品名称,
                                    a.转出数量
                                };
                int j = 0;

                if (myQuery1s.Count() == 0)
                {



                }
                else
                {



                    foreach (var myQuery1 in myQuery1s)
                    {
                        int month = Convert.ToInt32(myQuery1.会计期间ID.Substring(4, 2));
                        myExcelSheet.Cells[5, month + 3] = myQuery1.转出数量;
                        if (j == 0)
                        {
                            myExcelSheet.Cells[3, 2] = "(" + myQuery1.产品ID + ")" + myQuery1.产品名称;
                            j = j + 1;
                        }

                    }


                    var myQuery2s = from a in db.VABC_Report_ProductMeterialsCost
                                    where a.会计期间ID.StartsWith(MyYear)
                                    && a.产品ID == ProductId
                                    group a by new { a.会计期间ID, a.产品ID } into aa
                                    select new
                                    {
                                        aa.Key.产品ID,
                                        aa.Key.会计期间ID,
                                        金额 = aa.Sum(p => p.金额)

                                    };
                    Decimal[] materials = new Decimal[13];
                    Decimal[] activitys = new Decimal[13]{0,0,0,0,0,0,0,0,0,0,0,0,0};
                    foreach (var myQuery2 in myQuery2s)
                    {
                        int month = Convert.ToInt32(myQuery2.会计期间ID.Substring(4, 2));
                        myExcelSheet.Cells[6, month + 3] = myQuery2.金额;
                        materials[month] = Convert.ToDecimal(myQuery2.金额);
                    }
                    string[] hello = new string[] { "DF", "KY", "LD", "SF", "ZQ", "CL", "RG", "SB", "CF" };
                    //燃动费用
                    var MyQuery3s = from a in db.VABC_Report_ProductActiveCost
                                    where a.会计期间ID.StartsWith(MyYear)
                                    && a.产品ID == ProductId
                                    && new string[] { "DF", "KY", "LD", "SF", "ZQ" }.Contains(a.成本细项ID)
                                    group a by new { a.会计期间ID, a.产品ID } into aa
                                    select new
                                    {
                                        aa.Key.产品ID,
                                        aa.Key.会计期间ID,
                                        金额 = aa.Sum(p => p.金额)

                                    };
                    foreach (var myQuery3 in MyQuery3s)
                    {
                        int month = Convert.ToInt32(myQuery3.会计期间ID.Substring(4, 2));
                        myExcelSheet.Cells[8, month + 3] = myQuery3.金额;
                        activitys[month] =activitys[month]+Convert.ToDecimal(myQuery3.金额);
                    }

                    //人工费用
                    var MyQuery4s = from a in db.VABC_Report_ProductActiveCost
                                    where a.会计期间ID.StartsWith(MyYear)
                                    && a.产品ID == ProductId
                                   && new string[] { "RG" }.Contains(a.成本细项ID)
                                    group a by new { a.会计期间ID, a.产品ID } into aa
                                    select new
                                    {
                                        aa.Key.产品ID,
                                        aa.Key.会计期间ID,
                                        金额 = aa.Sum(p => p.金额)

                                    };
                    foreach (var myQuery4 in MyQuery4s)
                    {
                        int month = Convert.ToInt32(myQuery4.会计期间ID.Substring(4, 2));
                        myExcelSheet.Cells[10, month + 3] = myQuery4.金额;
                        activitys[month] = activitys[month] + Convert.ToDecimal(myQuery4.金额);
                    }

                    //折旧费用
                    var MyQuery5s = from a in db.VABC_Report_ProductActiveCost
                                    where a.会计期间ID.StartsWith(MyYear)
                                    && a.产品ID == ProductId
                                   && new string[] { "SB", "CF" }.Contains(a.成本细项ID)
                                    group a by new { a.会计期间ID, a.产品ID } into aa
                                    select new
                                    {
                                        aa.Key.产品ID,
                                        aa.Key.会计期间ID,
                                        金额 = aa.Sum(p => p.金额)

                                    };
                    foreach (var myQuery5 in MyQuery5s)
                    {
                        int month = Convert.ToInt32(myQuery5.会计期间ID.Substring(4, 2));
                        myExcelSheet.Cells[12, month + 3] = myQuery5.金额;
                        activitys[month] = activitys[month] + Convert.ToDecimal(myQuery5.金额);
                    }

                    var MyQuery6s = from a in db.VABC_Report_ActivityCostAllocation
                                    where a.会计期间ID.StartsWith(MyYear)
                                   && !hello.Contains(a.成本细项ID)
                                    group a by new { a.会计期间ID, a.成本细项ID } into aa
                                    select new
                                    {

                                        aa.Key.会计期间ID,
                                        aa.Key.成本细项ID,
                                        金额 = aa.Sum(p => p.金额)

                                    };
                    string[] GetDetail = (from a in MyQuery6s select a.成本细项ID).Distinct().ToArray();
                    int InsertRowNum = GetDetail.Count()*2;
                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[15, Missing];

                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);

                    }
                    myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[14, Missing];
                    myRange.RowHeight = 11;
                    myRange = null;

                    for (int i = 0; i < InsertRowNum; i=i+2)
                    {
                        int k = i / 2;
                        var Details = from a in MyQuery6s where a.成本细项ID == GetDetail[k] select a;
                        Decimal allmoney = 0;
                        foreach (var Detail in Details)
                        {
                            int month = Convert.ToInt32(Detail.会计期间ID.Substring(4, 2));
                            myExcelSheet.Cells[14 + i, month + 3] = Detail.金额;
                            activitys[month] = activitys[month] + Convert.ToDecimal(Detail.金额);
                            allmoney = Convert.ToDecimal(allmoney + Detail.金额);
                        }
                        string columnid = (from a in db.TABC_DM_CostItems where a.成本细项ID == GetDetail[k] select a.成本细项名称).Single().ToString();
                        myExcelSheet.Cells[14 + i, 1] = columnid;
                        myExcelSheet.Cells[14 + i, 2] = "成本金额";
                        myExcelSheet.Cells[14 + i, 3] = allmoney;
                        //myExcelSheet.Cells[10 + i, 2] = (from a in db.TABC_DM_CostItems where a.成本细项ID == GetDetail[i] select a.成本细项名称).Single().ToString(); ;
                    }
                    for (int i = 1; i < InsertRowNum; i = i + 2)
                    {
                        int k = i / 2;
                        var Details = from a in MyQuery6s where a.成本细项ID == GetDetail[k] select a;
                        foreach (var Detail in Details)
                        {
                            int month = Convert.ToInt32(Detail.会计期间ID.Substring(4, 2));
                            if (Convert.ToDecimal(((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[5, month + 2]).Value2) == 0)
                            {
                                myExcelSheet.Cells[14 + i, month + 2] = 0;
                            }
                            else
                            {
                                myExcelSheet.Cells[14 + i, month + 2] = Convert.ToDecimal(((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[14 + i - 1, month + 2]).Value2) / Convert.ToDecimal(((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[5, month + 2]).Value2);
                            }//allmoney = Convert.ToDecimal(allmoney + Detail.金额);
                            //((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[10+i, month * 2 + 2]).Formula=
                        }
                        //myExcelSheet.Cells[14 + i, 1] = GetDetail[k];
                        myExcelSheet.Cells[14 + i, 2] = "单位成本";
                        //myExcelSheet.Cells[10 + i, 2] = (from a in db.TABC_DM_CostItems where a.成本细项ID == GetDetail[i] select a.成本细项名称).Single().ToString(); ;
                    }
                    for (int i =1; i <= 12; i++)
                    {
                        myExcelSheet.Cells[14 + InsertRowNum +1, 3 + i] = materials[i] + activitys[i];
                    }

                    for (int i = 0; i < InsertRowNum; i += 2)
                    {
                        int k = i + 14;
                        myExcelSheet.get_Range("A"+k.ToString(), "A"+(k+1).ToString()).MergeCells = true; 
                    }
                    Range range = myExcelSheet.get_Range("A40".ToString(), Missing);
                    Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");
                    // myExcelSheet.Hyperlinks.Add
                }


                myExcelBook.Save();

                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch (Exception ee)
            {
                return ee.Message.ToString(); ;

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }
        }

        #endregion 



        #region//产品完工成本表
        public string WriteExcel_Report_CPWGCBB(string AccountPeriodValue, string UserName, string IsBaseReport)
        {

            string[] Myfile = ReturnfilePath(UserName, (IsBaseReport == "yes" ? "产品完工成本表_Base" : "产品完工成本表"));
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics((IsBaseReport == "yes" ? "产品完工成本表_Base" : "产品完工成本表"), UserName, AccountPeriodValue, DateTime.Now);
            ClearThreeObject();

            //引用的com组件：Microsoft.Office.Interop.Excel.dll   读取EXCEL文件
            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;

            int StartRow = 3;
            int j = 0;
            int SumRow = 0;//

            try
            {

                var MyProductIds = (from a in db.TABC_Report_ProductSumCost
                                    where a.会计期间ID == AccountPeriodValue
                                    && a.转出数量 > 0
                                    orderby a.产品ID
                                    select new
                                    {
                                        a.产品ID,
                                        a.产品名称,
                                        a.产品编码,
                                        a.转出数量,
                                        a.车间,
                                        // a.规格型号,
                                        a.计量单位,
                                        产品前三位 = a.产品ID.ToString().Substring(0, 3)
                                    }).Distinct();





                if (MyProductIds.Count() == 0)
                {


                }
                else
                {
                    List<string> Query =( from c in ((from m in db.TABC_Report_ProductSumCost
                                          join b in db.TABC_DM_CostItems  on m.成本细项ID equals b.成本细项ID
                                          where m.会计期间ID == AccountPeriodValue
                                          orderby b.报表顺序码 ascending
                                          select new
                                          {
                                              m.成本细项ID,
                                              b.报表顺序码
                                          }).Distinct()) orderby c.报表顺序码 select c.成本细项ID).ToList();
                     int InsertColumn = Query.Count();
                    List<TempLetters> Temp = new List<TempLetters>();
                    //for (int p = 0; p < InsertColumn; p++)
                    //{
                       // int num = Convert.ToInt32((from a in db.TABC_DM_CostItems where a.成本细项ID == Query[p] select a.报表顺序码).Single());
                       // Query2[num - ItemsALL + InsertColumn] = Query[p];
                    //}
                    int forcount=9;
                    Microsoft.Office.Interop.Excel.Range myRange2= (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Columns[9, Missing];
                    for (int k = 0; k< InsertColumn; k++)
                    {
                        myRange2.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlToRight, Missing);
                        //myRange2.Interior.ColorIndex = 19;
                        string name = (from a in db.TABC_DM_CostItems where a.成本细项ID == Query[k] select a).Single().成本细项名称;
                        myExcelSheet.Cells[2, forcount] =name;
                        TempLetters tl = new TempLetters();
                        tl.ID = Query[k];
                        tl.Number = forcount;
                        tl.letter = ((Letters)forcount).ToString();
                        tl.Name = name;
                        Temp.Add(tl);
                        forcount++;
                    }
                    myRange2 = null;
                        //插入会计期间。
                    myExcelSheet.Cells[1, 1] = "完工产品成本表(" + AccountPeriodValue + ")";

                    //计算一共有多少行数据行。
                    int InsertRowNum = MyProductIds.Count();
                    //计算需要插入多少个合计项的行。
                    int TotalRowNum = (from a in MyProductIds
                                       select new { a.产品前三位 }).Distinct().Count();
                    //计算一共需要插入的行数。
                    int AllRowNum = InsertRowNum + TotalRowNum;



                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StartRow + 1, Missing];
                    for (int i = 0; i < AllRowNum; i++)
                    {
                        myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    }
                    myRange = null;
                   


                    string FirstThreeNumber = "";
                    foreach (var MyProductId in MyProductIds)
                    {
                        if ((StartRow + j) == SumRow)
                        {
                            //判断当前行是合计行的时候，向下跳转一行。
                            j++;
                        }
                        //判断当前的分组字段是否改变
                        if (FirstThreeNumber != MyProductId.产品前三位)
                        {
                            int MyRowCount = (from a in MyProductIds
                                              where a.产品前三位 == MyProductId.产品前三位
                                              select a).Count();



                            myExcelSheet.Cells[StartRow + j + MyRowCount, 1] = "小计：";

                            Microsoft.Office.Interop.Excel.Range myRange1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.get_Range(myExcelSheet.Cells[StartRow + j + MyRowCount, 1], myExcelSheet.Cells[StartRow + j + MyRowCount, 24]);
                            myRange1.Interior.ColorIndex = 19;
                            myRange1 = null;

                            SumRow = StartRow + j + MyRowCount;

                            FirstThreeNumber = MyProductId.产品前三位;
                            for (int n = 8; n < 9 + InsertColumn; n++)
                            {
                                string tem = ((Letters)n).ToString();
                                ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, n]).Formula =
                                    "=" + "sum("+tem + (StartRow + j) + ":" + tem + (StartRow + j + MyRowCount - 1);
                            }
                              

                        }



                        #region 插入材料和作业成本。

                        myExcelSheet.Cells[StartRow + j, 1] = MyProductId.车间;
                        myExcelSheet.Cells[StartRow + j, 2] = MyProductId.产品ID;
                        myExcelSheet.Cells[StartRow + j, 3] = "(" + MyProductId.产品编码 + ")" + MyProductId.产品名称;
                        //   myExcelSheet.Cells[StartRow + j, 4] = MyProductId.规格型号;
                        myExcelSheet.Cells[StartRow + j, 5] = MyProductId.计量单位;
                        myExcelSheet.Cells[StartRow + j, 6] = MyProductId.转出数量;

                        //var MyMeterialSum = from h in myMeterialSums
                        //                    where h.Key == MyProductId.产品ID
                        //                    select h;

                        //if (MyMeterialSum.Count() == 1)
                        //{
                        //    var _MyMeterialSum = MyMeterialSum.Single();
                        //    myExcelSheet.Cells[StartRow + j, 9] = _MyMeterialSum.材料成本;
                        //}
                        //else if (MyMeterialSum.Count() == 0)
                        //{
                        //    myExcelSheet.Cells[StartRow + j, 9] = 0;
                        //}



                        var ProductQuerys1 = from m in db.TABC_Report_ProductSumCost
                                             where m.会计期间ID == AccountPeriodValue
                                             && m.产品ID == MyProductId.产品ID
                                             select m;


                        foreach (var ProductQuery1 in ProductQuerys1)
                        {
                            string TempLe = ((Letters)9 + InsertColumn).ToString();
                            //定义单位成本的计算公式
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j, 7]).Formula = "=" + "H" + (StartRow + j) + "/" + "F" + (StartRow + j);

                            //定义总成本计算工式。将所有细项数据汇总。
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j, 8]).Formula = "=" + "sum(" + "I" + (StartRow + j) + ":" + TempLe + (StartRow + j) + ")";
                            foreach (var deta in Temp)
                            {
                                if (ProductQuery1.成本细项ID == deta.ID)
                                {
                                    myExcelSheet.Cells[StartRow + j, deta.Number] = Math.Round((decimal)ProductQuery1.金额, 2, MidpointRounding.AwayFromZero);
                                }
                            }




                        }
                        #endregion



                        j++;
                    }
                    decimal TotalAll = 0;
                    foreach (var temp2 in Temp)
                    {
                        decimal TotalMoney = (decimal)(from b in db.TABC_Report_ProductSumCost
                                                               where b.会计期间ID == AccountPeriodValue && b.成本细项ID == temp2.ID
                                                               select
                                                                    (decimal)Math.Round((decimal)b.金额, 2, MidpointRounding.AwayFromZero)
                                                           ).Sum();
                        myExcelSheet.Cells[StartRow + j + 3, temp2.Number] = TotalMoney;
                        TotalAll += TotalMoney;
                    }
                    myExcelSheet.Cells[StartRow + j + 3, 8] = TotalAll;

                }


                myExcelBook.Save();



                Range range = myExcelSheet.get_Range(("A" + (StartRow + j + 4).ToString()).ToString(), Missing);
                Hyperlink hyperlink1 = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");
                Range range_Printe = myExcelSheet.get_Range(("B" + (StartRow + j + 4).ToString()).ToString(), Missing);
                Hyperlink hyperlink2 = (Hyperlink)range_Printe.Hyperlinks.Add(range_Printe, "/Report/UserReport/" + StrReportName + ".xls", Missing, Missing, "打印");

                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion

        #region//非正常转出成本表

        

        public string WriteExcel_Report_FZCZCCBB(string AccountPeriodValue, string UserName,string IsBaseReport)
        {

            string[] Myfile = ReturnfilePath(UserName, (IsBaseReport == "yes" ? "非正常转出成本表_Base" : "非正常转出成本表"));
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics((IsBaseReport == "yes" ? "非正常转出成本表_Base" : "非正常转出成本表"), UserName, AccountPeriodValue, DateTime.Now);
            ClearThreeObject();


            //引用的com组件：Microsoft.Office.Interop.Excel.dll   读取EXCEL文件
            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;

            int StartRow = 3;
            int j = 0;
            int SumRow = 0;//

            try
            {

                var MyProductIds = (from a in db.TABC_Report_ProductInProcessSumCost
                                    join b in db.TABC_JS_Product on a.产品ID equals b.产品账户ID
                                    where a.会计期间ID == AccountPeriodValue && !a.产品ID.StartsWith("96")
                                    && a.损失金额!= 0
                                    orderby a.产品ID
                                    select new
                                    {
                                        a.产品ID,
                                        b.产品账户名称,
                                        a.产品数量,
                                        a.车间,
                                        a.损失金额,
                                        b.规格型号,
                                        b.计量单位,
                                        产品前三位 = a.产品ID.ToString().Substring(0, 3)
                                    }).Distinct();

                var myMeterialSums = (from b in db.VABC_Report_ProductMeterialsCost
                                      where b.会计期间ID == AccountPeriodValue && !b.产品ID.StartsWith("96")
                                      group b by b.产品ID into g
                                      select new
                                      {
                                          g.Key,
                                          损失金额 = g.Sum(b => b.损失金额)

                                      }).ToList();


                if (MyProductIds.Count() == 0)
                {


                }
                else
                {
                    List<string> Query = (from c in
                                              ((from m in db.TABC_Report_ProductSumCost
                                                join b in db.TABC_DM_CostItems on m.成本细项ID equals b.成本细项ID
                                                where m.会计期间ID == AccountPeriodValue 
                                                orderby b.报表顺序码 ascending
                                                select new
                                                {
                                                    m.成本细项ID,
                                                    b.报表顺序码
                                                }).Distinct())
                                          orderby c.报表顺序码
                                          select c.成本细项ID).ToList();
                    int InsertColumn = Query.Count();
                    List<TempLetters> Temp = new List<TempLetters>();
                    //for (int p = 0; p < InsertColumn; p++)
                    //{
                    // int num = Convert.ToInt32((from a in db.TABC_DM_CostItems where a.成本细项ID == Query[p] select a.报表顺序码).Single());
                    // Query2[num - ItemsALL + InsertColumn] = Query[p];
                    //}
                    int forcount = 9;
                    Microsoft.Office.Interop.Excel.Range myRange2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Columns[9, Missing];
                    for (int k = 0; k < InsertColumn; k++)
                    {
                        myRange2.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlToRight, Missing);
                        //myRange2.Interior.ColorIndex = 19;
                        string name = (from a in db.TABC_DM_CostItems where a.成本细项ID == Query[k] select a).Single().成本细项名称;
                        myExcelSheet.Cells[2, forcount] = name;
                        TempLetters tl = new TempLetters();
                        tl.ID = Query[k];
                        tl.Number = forcount;
                        tl.letter = ((Letters)forcount).ToString();
                        tl.Name = name;
                        Temp.Add(tl);
                        forcount++;
                    }
                    myRange2 = null;
                    //插入会计期间。
                    myExcelSheet.Cells[1, 1] = "非正常转出成本表(" + AccountPeriodValue + ")";

                    //计算一共有多少行数据行。
                    int InsertRowNum = MyProductIds.Count();
                    //计算需要插入多少个合计项的行。
                    int TotalRowNum = (from a in MyProductIds
                                       select new { a.产品前三位 }).Distinct().Count();
                    //计算一共需要插入的行数。
                    int AllRowNum = InsertRowNum + TotalRowNum;



                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StartRow + 1, Missing];
                    for (int i = 0; i < AllRowNum; i++)
                    {
                        myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    }
                    myRange = null;



                    string FirstThreeNumber = "";
                    foreach (var MyProductId in MyProductIds)
                    {
                        if ((StartRow + j) == SumRow)
                        {
                            //判断当前行是合计行的时候，向下跳转一行。
                            j++;
                        }
                        //判断当前的分组字段是否改变
                        if (FirstThreeNumber != MyProductId.产品前三位)
                        {
                            int MyRowCount = (from a in MyProductIds
                                              where a.产品前三位 == MyProductId.产品前三位
                                              select a).Count();



                            myExcelSheet.Cells[StartRow + j + MyRowCount, 1] = "小计：";

                            Microsoft.Office.Interop.Excel.Range myRange1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.get_Range(myExcelSheet.Cells[StartRow + j + MyRowCount, 1], myExcelSheet.Cells[StartRow + j + MyRowCount, 24]);
                            myRange1.Interior.ColorIndex = 19;
                            myRange1 = null;

                            SumRow = StartRow + j + MyRowCount;

                            FirstThreeNumber = MyProductId.产品前三位;
                            for (int n = 8; n < 9 + InsertColumn; n++)
                            {
                                string tem = ((Letters)n).ToString();
                                ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, n]).Formula =
                                    "=" + "sum(" + tem + (StartRow + j) + ":" + tem + (StartRow + j + MyRowCount - 1);
                            }


                        }



                        #region 插入材料和作业成本。

                        myExcelSheet.Cells[StartRow + j, 1] = MyProductId.车间;
                        myExcelSheet.Cells[StartRow + j, 2] = MyProductId.产品ID;
                        myExcelSheet.Cells[StartRow + j, 3] = MyProductId.产品账户名称;
                        myExcelSheet.Cells[StartRow + j, 4] = MyProductId.规格型号;
                        myExcelSheet.Cells[StartRow + j, 5] = MyProductId.计量单位;
                        myExcelSheet.Cells[StartRow + j, 6] = 0;
                        var MyMeterialSum = from h in myMeterialSums
                                            where h.Key == MyProductId.产品ID
                                            select h;

                        if (MyMeterialSum.Count() == 1)
                        {
                            var _MyMeterialSum = MyMeterialSum.Single();
                            myExcelSheet.Cells[StartRow + j, 9] = _MyMeterialSum.损失金额;
                        }
                        else if (MyMeterialSum.Count() == 0)
                        {
                            myExcelSheet.Cells[StartRow + j, 9] = 0;
                        }
                        
                        var ProductQuerys1 = from m in db.VABC_Report_ProductActiveCost
                                             where m.会计期间ID == AccountPeriodValue
                                             && m.产品ID == MyProductId.产品ID && !m.产品ID.StartsWith("96")
                                             select m;


                        foreach (var ProductQuery1 in ProductQuerys1)
                        {
                            string TempLe = ((Letters)9 + InsertColumn).ToString();
                            

                            //定义总成本计算工式。将所有细项数据汇总。
                            //Decimal ALLMoney = 0;
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j, 8]).Formula = "=" + "sum(" + "I" + (StartRow + j) + ":" + TempLe + (StartRow + j) + ")";
                            foreach (var deta in Temp)
                            {
                                if (ProductQuery1.成本细项ID == deta.ID)
                                {
                                    Decimal TempMoney = Math.Round((decimal)ProductQuery1.损失金额, 2, MidpointRounding.AwayFromZero);
                                    //ALLMoney = ALLMoney + TempMoney;                        
                                    myExcelSheet.Cells[StartRow + j, deta.Number] = TempMoney;
                                    Temp[deta.Number - 9].Money = Temp[deta.Number - 9].Money + TempMoney;
                                }
                            }




                        }
                        #endregion
                        j++;
                    }
                    decimal TotalAll = 0;
                    foreach (var temp2 in Temp)
                    {
                        if (temp2.ID != "CL")
                        {
                            //string detail = temp2.ID;
                            //var TEM = (from b in db.VABC_Report_ProductActiveCost
                                       //where b.会计期间ID == AccountPeriodValue && b.成本细项ID == detail && !b.产品ID.StartsWith("96")
                                       //select b.损失金额).Sum();


                            decimal TotalMoney = Convert.ToDecimal(temp2.Money);
                                //(from b in db.VABC_Report_ProductActiveCost
                                                           //where b.会计期间ID == AccountPeriodValue && b.成本细项ID ==detail && !b.产品ID.StartsWith("96")
                                                           //select
                                                                //(decimal)Math.Round((decimal)b.损失金额, 2, MidpointRounding.AwayFromZero)
                                                           //).Single();
                            myExcelSheet.Cells[StartRow + j + 3, temp2.Number] = TotalMoney;
                            TotalAll += TotalMoney;
                        }
                        
                    }
                    var TotalMeterialMoney = (from b in db.VABC_Report_ProductMeterialsCost
                                              where b.会计期间ID == AccountPeriodValue && !b.产品ID.StartsWith("96")
                                              select b.损失金额).Sum();

                    decimal meterial = 0;
                    meterial = (decimal)(TotalMeterialMoney.ToString() == "" ? 0 : TotalMeterialMoney);
                    myExcelSheet.Cells[StartRow + j + 3, 9] = meterial;
                    myExcelSheet.Cells[StartRow + j + 3, 8] = TotalAll+meterial;

                }


                myExcelBook.Save();



                Range range = myExcelSheet.get_Range(("A" + (StartRow + j + 4).ToString()).ToString(), Missing);
                Hyperlink hyperlink1 = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");
                Range range_Printe = myExcelSheet.get_Range(("B" + (StartRow + j + 4).ToString()).ToString(), Missing);
                Hyperlink hyperlink2 = (Hyperlink)range_Printe.Hyperlinks.Add(range_Printe, "/Report/UserReport/" + StrReportName + ".xls", Missing, Missing, "打印");

                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch(Exception ee)
            {
                return ee.Message.ToString(); ;

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }



        }

        #endregion



        #region//非正常转出成本表之其他成本表，ID为96开头的。


        public string WriteExcel_Report_FZCZCCBB_Other(string AccountPeriodValue, string UserName,string IsBaseReport)
        {

            string[] Myfile = ReturnfilePath(UserName,  (IsBaseReport == "yes"?"其他业务转出成本表_Base":"其他业务转出成本表"));
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics((IsBaseReport == "yes" ? "其他业务转出成本表_Base" : "其他业务转出成本表"), UserName, AccountPeriodValue, DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {

                var MyProductIds = (from a in db.TABC_Report_ProductInProcessSumCost
                                   join b in db.TABC_JS_Product on a.产品ID equals b.产品账户ID
                                   where a.会计期间ID == AccountPeriodValue && a.产品ID.StartsWith("96")
                                   && a.损失金额 != 0
                                   orderby a.产品ID
                                   select new
                                   {
                                       a.产品ID,
                                       b.产品账户名称,
                                       a.产品数量,
                                       a.车间,
                                       b.规格型号,
                                       b.计量单位,
                                       产品前三位 = a.产品ID.ToString().Substring(0, 3)
                                   }).ToList();

                var myMeterialSums = (from b in db.VABC_Report_ProductMeterialsCost
                                     where b.会计期间ID == AccountPeriodValue && b.产品ID.StartsWith("96")
                                     group b by b.产品ID into g
                                     select new
                                     {
                                         g.Key,
                                         损失金额 = g.Sum(b => b.损失金额)

                                     }).ToList();




                if (MyProductIds.Count() == 0)
                {


                }
                else
                {
                    //计算一共有多少行数据行。
                    int InsertRowNum = MyProductIds.Count();
                    //计算需要插入多少个合计项的行。
                    int TotalRowNum = (from a in MyProductIds
                                       select new { a.产品前三位 }).Distinct().Count();
                    //计算一共需要插入的行数。
                    int AllRowNum = InsertRowNum + TotalRowNum;

                    int StartRow = 3;
                    int j = 0;
                    int SumRow = 0;

                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StartRow + 1, Missing];
                    for (int i = 0; i < AllRowNum; i++)
                    {
                        myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                    }
                    myRange = null;

                    decimal TotalCF = 0, TotalDF = 0, TotalDX = 0, TotalKY = 0, TotalRG = 0, TotalSB = 0, TotalSF = 0, TotalWS = 0, TotalZQ = 0, TotalLD = 0, TotalZZ = 0, TotalRM = 0, TotalZJ = 0, TotalXZ = 0, TotalCC = 0;
        

                    string FirstThreeNumber = "";
                    foreach (var MyProductId in MyProductIds)
                    {
                        if ((StartRow + j) == SumRow)
                        {
                            //判断当前行是合计行的时候，向下跳转一行。
                            j++;
                        }
                        //判断当前的分组字段是否改变
                        if (FirstThreeNumber != MyProductId.产品前三位)
                        {
                            int MyRowCount = (from a in MyProductIds
                                              where a.产品前三位 == MyProductId.产品前三位
                                              select a).Count();



                            myExcelSheet.Cells[StartRow + j + MyRowCount, 1] = "小计：";

                            Microsoft.Office.Interop.Excel.Range myRange1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.get_Range(myExcelSheet.Cells[StartRow + j + MyRowCount, 1], myExcelSheet.Cells[StartRow + j + MyRowCount, 24]);
                            myRange1.Interior.ColorIndex = 19;
                            myRange1 = null;

                            SumRow = StartRow + j + MyRowCount;

                            FirstThreeNumber = MyProductId.产品前三位;

                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 8]).Formula =
                                "=" + "sum(H" + (StartRow + j) + ":" + "H" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 9]).Formula =
                               "=" + "sum(I" + (StartRow + j) + ":" + "I" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 10]).Formula =
                               "=" + "sum(J" + (StartRow + j) + ":" + "J" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 11]).Formula =
                               "=" + "sum(K" + (StartRow + j) + ":" + "K" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 12]).Formula =
                               "=" + "sum(L" + (StartRow + j) + ":" + "L" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 13]).Formula =
                               "=" + "sum(M" + (StartRow + j) + ":" + "M" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 14]).Formula =
                               "=" + "sum(N" + (StartRow + j) + ":" + "N" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 15]).Formula =
                               "=" + "sum(O" + (StartRow + j) + ":" + "O" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 16]).Formula =
                               "=" + "sum(P" + (StartRow + j) + ":" + "P" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 17]).Formula =
                               "=" + "sum(Q" + (StartRow + j) + ":" + "Q" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 18]).Formula =
                               "=" + "sum(R" + (StartRow + j) + ":" + "R" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 19]).Formula =
                               "=" + "sum(S" + (StartRow + j) + ":" + "S" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 20]).Formula =
                               "=" + "sum(T" + (StartRow + j) + ":" + "T" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 21]).Formula =
                               "=" + "sum(U" + (StartRow + j) + ":" + "U" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 22]).Formula =
                               "=" + "sum(V" + (StartRow + j) + ":" + "V" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 23]).Formula =
                               "=" + "sum(W" + (StartRow + j) + ":" + "W" + (StartRow + j + MyRowCount - 1);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j + MyRowCount, 24]).Formula =
                               "=" + "sum(X" + (StartRow + j) + ":" + "X" + (StartRow + j + MyRowCount - 1);

                        }



                        #region 插入材料和作业成本。

                        myExcelSheet.Cells[StartRow + j, 1] = MyProductId.车间;
                        myExcelSheet.Cells[StartRow + j, 2] = MyProductId.产品ID;
                        myExcelSheet.Cells[StartRow + j, 3] = MyProductId.产品账户名称;
                        myExcelSheet.Cells[StartRow + j, 4] = MyProductId.规格型号;
                        myExcelSheet.Cells[StartRow + j, 5] = MyProductId.计量单位;
                        myExcelSheet.Cells[StartRow + j, 6] = 0;

                        var MyMeterialSum = from h in myMeterialSums
                                            where h.Key == MyProductId.产品ID
                                            select h;

                        if (MyMeterialSum.Count() == 1)
                        {
                            var _MyMeterialSum = MyMeterialSum.Single();
                            myExcelSheet.Cells[StartRow + j, 9] = _MyMeterialSum.损失金额;
                        }
                        else if (MyMeterialSum.Count() == 0)
                        {
                            myExcelSheet.Cells[StartRow + j, 9] = 0;
                        }



                        var ProductQuerys1 = from m in db.VABC_Report_ProductActiveCost
                                             where m.会计期间ID == AccountPeriodValue
                                             && m.产品ID == MyProductId.产品ID
                                             select m;


                        foreach (var ProductQuery1 in ProductQuerys1)
                        {

                            //定义单位成本的计算公式
                        //    ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j, 7]).Formula = "=" + "H" + (StartRow + j) + "/" + "F" + (StartRow + j);

                            //定义总成本计算工式。将所有细项数据汇总。
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StartRow + j, 8]).Formula = "=" + "sum(" + "I" + (StartRow + j) + ":" + "X" + (StartRow + j) + ")";

                            switch (ProductQuery1.成本细项ID)
                            {
                                case "CF"://厂房
                                    {
                                        myExcelSheet.Cells[StartRow + j, 12] = ProductQuery1.损失金额;
                                        TotalCF = TotalCF + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "DF"://电费
                                    {
                                        myExcelSheet.Cells[StartRow + j, 15] = ProductQuery1.损失金额;
                                        TotalDF = TotalDF + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "DX"://机修
                                    {
                                        myExcelSheet.Cells[StartRow + j, 20] = ProductQuery1.损失金额;
                                        TotalDX = TotalDX + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "KY"://空压
                                    {
                                        myExcelSheet.Cells[StartRow + j, 17] = ProductQuery1.损失金额;
                                        TotalKY = TotalKY + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "RG"://人工
                                    {
                                        myExcelSheet.Cells[StartRow + j, 10] = ProductQuery1.损失金额;
                                        TotalRG = TotalRG + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "SB"://设备
                                    {
                                        myExcelSheet.Cells[StartRow + j, 11] = ProductQuery1.损失金额;
                                        TotalSB = TotalSB + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "SF"://水费
                                    {
                                        myExcelSheet.Cells[StartRow + j, 14] = ProductQuery1.损失金额;
                                        TotalSF = TotalSF + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "WS"://污水处理
                                    {
                                        myExcelSheet.Cells[StartRow + j, 19] = ProductQuery1.损失金额;
                                        TotalWS = TotalWS + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "ZQ":// 蒸汽
                                    {
                                        myExcelSheet.Cells[StartRow + j, 16] = ProductQuery1.损失金额;
                                        TotalZQ = TotalZQ + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "LD"://冷冻
                                    {
                                        myExcelSheet.Cells[StartRow + j, 18] = ProductQuery1.损失金额;
                                        TotalLD = TotalLD + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "ZZ"://协调
                                    {
                                        myExcelSheet.Cells[StartRow + j, 13] = ProductQuery1.损失金额;
                                        TotalZZ = TotalZZ + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "RM"://溶媒
                                    {
                                        myExcelSheet.Cells[StartRow + j, 21] = ProductQuery1.损失金额;
                                        TotalRM = TotalRM + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "ZJ"://质检
                                    {
                                        myExcelSheet.Cells[StartRow + j, 22] = ProductQuery1.损失金额;
                                        TotalZJ = TotalZJ + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "XZ"://闲置
                                    {
                                        myExcelSheet.Cells[StartRow + j, 23] = ProductQuery1.损失金额;
                                        TotalXZ = TotalXZ + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                                case "CC"://仓储
                                    {
                                        myExcelSheet.Cells[StartRow + j, 24] = ProductQuery1.损失金额;
                                        TotalCC = TotalCC + (decimal)ProductQuery1.损失金额;
                                        break;
                                    }
                            }



                        }
                        #endregion



                        j++;
                    }



                    var TotalMeterialMoney = (from b in db.VABC_Report_ProductMeterialsCost
                                                           where b.会计期间ID == AccountPeriodValue && b.产品ID.StartsWith("96")
                                                           select b.损失金额).Sum();
                    myExcelSheet.Cells[StartRow + j + 3, 9] = (TotalMeterialMoney.ToString() == "" ? 0:TotalMeterialMoney);



                    myExcelSheet.Cells[StartRow + j + 3, 10] = (TotalRG.ToString() == "" ? 0 : TotalRG); 

  

                    myExcelSheet.Cells[StartRow + j + 3, 11] = (TotalSB.ToString() == "" ? 0 : TotalSB);



                    myExcelSheet.Cells[StartRow + j + 3, 12] = (TotalCF.ToString() == "" ? 0 : TotalCF);



                    myExcelSheet.Cells[StartRow + j + 3, 13] = (TotalZZ.ToString() == "" ? 0 : TotalZZ);



                    myExcelSheet.Cells[StartRow + j + 3, 14] = (TotalSF.ToString() == "" ? 0 : TotalSF);

    

                    myExcelSheet.Cells[StartRow + j + 3, 15] = (TotalDF.ToString() == "" ? 0 : TotalDF);

   

                    myExcelSheet.Cells[StartRow + j + 3, 16] = (TotalZQ.ToString() == "" ? 0 : TotalZQ);

            

                    myExcelSheet.Cells[StartRow + j + 3, 17] = (TotalKY.ToString() == "" ? 0 : TotalKY);



                    myExcelSheet.Cells[StartRow + j + 3, 18] = (TotalLD.ToString() == "" ? 0 : TotalLD);



                    myExcelSheet.Cells[StartRow + j + 3, 19] = (TotalWS.ToString() == "" ? 0 : TotalWS);



                    myExcelSheet.Cells[StartRow + j + 3, 20] = (TotalDX.ToString() == "" ? 0 : TotalDX);

 

                    myExcelSheet.Cells[StartRow + j + 3, 21] = (TotalRM.ToString() == "" ? 0 : TotalRM);

 

                    myExcelSheet.Cells[StartRow + j + 3, 22] = (TotalZJ.ToString() == "" ? 0 : TotalZJ);



                    myExcelSheet.Cells[StartRow + j + 3, 23] = (TotalXZ.ToString() == "" ? 0 : TotalXZ);

    

                    myExcelSheet.Cells[StartRow + j + 3, 24] = (TotalCC.ToString() == "" ? 0 : TotalCC);

                    Range range = myExcelSheet.get_Range(("A" + (StartRow + j + 4).ToString()).ToString(), Missing);
                    Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");


                }

                myExcelBook.Save();

                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion

        #region //生产费用明细表


        public string WriteExcel_Report_SCFYMXB(string MyYear, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "生产费用明细表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics("生产费用明细表", UserName, MyYear, DateTime.Now);
            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {
                //材料
                var myQuery1s = from a in db.VABC_Report_MeterialsCostAllocation
                                where a.会计期间ID.StartsWith(MyYear)
                                
                                group a by new { a.会计期间ID } into aa
                                select new
                                {
                                    key = aa.Key,
                                    aa.Key.会计期间ID,
                                    金额 = aa.Sum(p=>p.金额)

                                };

                foreach (var myQuery1 in myQuery1s)
                {
                    int month = Convert.ToInt32(myQuery1.会计期间ID.Substring(4, 2));
                    myExcelSheet.Cells[4, month + 2] = myQuery1.金额;
                }


                string[] hello = new string[] { "DF", "KY", "LD", "SF", "ZQ", "CL", "RG", "SB", "CF" };
                //燃动费用
                var MyQuery3s = from a in db.VABC_Report_ActivityCostAllocation
                                where a.会计期间ID.StartsWith(MyYear)
                                && new string[] { "DF", "KY", "LD", "SF", "ZQ" }.Contains(a.成本细项ID)
                                group a by new { a.会计期间ID } into aa
                                select new
                                {

                                    aa.Key.会计期间ID,
                                    金额 = aa.Sum(p => p.金额)

                                };
                foreach (var myQuery3 in MyQuery3s)
                {
                    int month = Convert.ToInt32(myQuery3.会计期间ID.Substring(4, 2));
                    myExcelSheet.Cells[5, month + 2] = myQuery3.金额;
                }

                //人工费用
                var MyQuery4s = from a in db.VABC_Report_ActivityCostAllocation
                                where a.会计期间ID.StartsWith(MyYear)
                               && new string[] { "RG" }.Contains(a.成本细项ID)
                                group a by new { a.会计期间ID } into aa
                                select new
                                {

                                    aa.Key.会计期间ID,
                                    金额 = aa.Sum(p => p.金额)

                                };
                foreach (var myQuery4 in MyQuery4s)
                {
                    int month = Convert.ToInt32(myQuery4.会计期间ID.Substring(4, 2));
                    myExcelSheet.Cells[6, month + 2] = myQuery4.金额;
                }

                //折旧费用
                var MyQuery5s = from a in db.VABC_Report_ActivityCostAllocation
                                where a.会计期间ID.StartsWith(MyYear)
                               && new string[] { "SB", "CF" }.Contains(a.成本细项ID)
                                group a by new { a.会计期间ID, a.成本细项ID } into aa
                                select new
                                {

                                    aa.Key.会计期间ID,
                                    aa.Key.成本细项ID,
                                    金额 = aa.Sum(p => p.金额)

                                };
                foreach (var myQuery5 in MyQuery5s)
                {
                    int month = Convert.ToInt32(myQuery5.会计期间ID.Substring(4, 2));
                    myExcelSheet.Cells[7, month + 2] = myQuery5.金额;
                }
                var MyQuery6s = from a in db.VABC_Report_ActivityCostAllocation
                                where a.会计期间ID.StartsWith(MyYear)
                               && !hello.Contains(a.成本细项ID)
                                group a by new { a.会计期间ID, a.成本细项ID } into aa
                                select new
                                {

                                    aa.Key.会计期间ID,
                                    aa.Key.成本细项ID,
                                    金额 = aa.Sum(p => p.金额)

                                };
                string[] GetDetail = (from a in MyQuery6s select a.成本细项ID).Distinct().ToArray();
                int InsertRowNum = GetDetail.Count();
                Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[9, Missing];

                for (int i = 0; i < InsertRowNum - 1; i++)
                {
                    myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);

                }
                myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[8, Missing];
                myRange.RowHeight = 11;
                myRange = null;

                for (int i = 0; i < InsertRowNum; i++)
                {
                    var Details = from a in MyQuery6s where a.成本细项ID == GetDetail[i] select a;
                    foreach (var Detail in Details)
                    {
                        int month = Convert.ToInt32(Detail.会计期间ID.Substring(4, 2));
                        myExcelSheet.Cells[8+i, month+2] = Detail.金额;
                    }

                    myExcelSheet.Cells[8+i, 2] = (from a in db.TABC_DM_CostItems where a.成本细项ID == GetDetail[i] select a.成本细项名称).Single().ToString(); ;
                }

                myExcelSheet.Cells[2, 3] = MyYear;
                Range range = myExcelSheet.get_Range("A17".ToString(), Missing);
                Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");




                myExcelBook.Save();
                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion

        #region//在产品静态分析表

        
        public string WriteExcel_Report_ZCPJTFXB(int CenterId, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "在产品静态分析表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];
            //写入报表点击查询页面
            WriteReportStatistics("在产品静态分析表", UserName, CenterId.ToString(), DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {

                var myQuerys1 = (from a in db.TABC_JS_CostCenter where a.上级作业中心ID == CenterId select a).ToArray();

                if (myQuerys1.Count() == 0)
                {
                    myExcelSheet.Cells[5, 1] = "没有满足查询的记录";

                }
                else
                {

                    myExcelSheet.Cells[2, 2] = "hisun";
                    myExcelSheet.Cells[3, 2] = myQuerys1[0].上级作业中心ID.ToString();
                    myExcelSheet.Cells[3, 4] = "测试";
                    int mySumCount = myQuerys1.Count();
                    int StartRow = 6;

                    for (int i = 0; i < mySumCount; i++)
                    {
                        myExcelSheet.Cells[StartRow + i, 1] = myQuerys1[i].作业中心ID.ToString();
                        myExcelSheet.Cells[StartRow + i, 2] = myQuerys1[i].作业中心名称.ToString();
                        myExcelSheet.Cells[StartRow + i, 3] = myQuerys1[i].类型.ToString();
                        myExcelSheet.Cells[StartRow + i, 4] = myQuerys1[i].是否直接成本.ToString();
                        myExcelSheet.Cells[StartRow + i, 5] = myQuerys1[i].成本细项ID.ToString();

                    }
                }



                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion

        #region//产品成本变动分析表
        public string WriteExcel_Report_CPCBBDFXB(string Ap1,string Ap2, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "产品成本变动分析表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics("产品成本变动分析表", UserName, Ap1.ToString() + "-" + Ap2.ToString(), DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {

                var myQuery1s = from m in
                                    (from a in db.VABC_Report_ProductSumCost where a.会计期间ID == Ap1 select a)
                                join n in
                                    (from b in db.VABC_Report_ProductSumCost where b.会计期间ID == Ap2 select b)
                                on m.产品ID equals n.产品ID
                                join h in db.TABC_JS_Product on m.产品ID equals h.产品账户ID
                                select new
                                {
                                    m.产品ID,
                                    m.产品名称,
                                    h.计量单位,
                                    m.产品数量,
                                    m.金额,
                                    比较期单位成本 = (n.产品数量 == 0 ? 0:n.金额/n.产品数量)
                                };

                myExcelSheet.Cells[2,5] = "本期成本"+"("+Ap1+")";
                myExcelSheet.Cells[2, 7] = "对照期成本" + "(" + Ap2 + ")";


                if (myQuery1s.Count() == 0)
                {
                    myExcelSheet.Cells[5, 1] = "没有满足查询的记录";

                }
                else
                {


                    int InsertRowNum = myQuery1s.Count();
                        Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[5, Missing];
                        for (int i = 0; i < InsertRowNum; i++)
                        {
                            myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                        }
                        myRange = null;


               
                    int StartRow = 4;
                    int j = 0;

                    foreach (var myQuery1 in myQuery1s)
                    {
                        myExcelSheet.Cells[StartRow + j, 1] = myQuery1.产品ID;
                        //Range range = myExcelSheet.get_Range("A" + (StartRow + j).ToString().ToString(), Missing);
                        //Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:location.href='../../view/reportquery/report_cpcbmxbdfxb.aspx?"+"ProductId=" + myQuery1.产品ID+"&ap1="+Ap1+"&ap2="+Ap2+"';", Missing, Missing, myQuery1.产品ID.ToString());

                        myExcelSheet.Cells[StartRow + j, 2] = myQuery1.产品名称;
                        myExcelSheet.Cells[StartRow + j, 3] = myQuery1.计量单位;
                        myExcelSheet.Cells[StartRow + j, 4] = myQuery1.产品数量;
                        myExcelSheet.Cells[StartRow + j, 6] = myQuery1.金额;
                        myExcelSheet.Cells[StartRow + j, 7] = myQuery1.比较期单位成本;
                        myExcelSheet.Cells[StartRow + j, 5] = "=if(iserror(f" + (StartRow + j).ToString() + "/" + "d" + (StartRow + j).ToString()
                                                                + "),0," + "f" + (StartRow + j).ToString() + "/" + "d" + (StartRow + j).ToString()+")";
                        myExcelSheet.Cells[StartRow + j, 8] = "=e" + (StartRow + j).ToString() + "-g" + (StartRow + j).ToString();
                        myExcelSheet.Cells[StartRow + j, 9] = "=if(iserror(h" + (StartRow + j).ToString() + "/" + "g" + (StartRow + j).ToString() +")" + ",0,h" + (StartRow + j).ToString() + "/" + 
                                                                 "g" + (StartRow + j).ToString() + ")";

                        j++;
                    }

                    Range range = myExcelSheet.get_Range("A"+(StartRow + j+2).ToString(), Missing);
                    Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");

                }

                myExcelBook.Save();

                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }
        #endregion

        #region //产品明细变动分析表

       
        public string WriteExcel_Report_CPMXBDB(int CenterId, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "产品明细变动表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];
            //写入报表点击查询页面
            WriteReportStatistics("产品明细变动表", UserName, CenterId.ToString(), DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {

                var myQuerys1 = (from a in db.TABC_JS_CostCenter where a.上级作业中心ID == CenterId select a).ToArray();

                if (myQuerys1.Count() == 0)
                {
                    myExcelSheet.Cells[5, 1] = "没有满足查询的记录";

                }
                else
                {

                    myExcelSheet.Cells[2, 2] = "hisun";
                    myExcelSheet.Cells[3, 2] = myQuerys1[0].上级作业中心ID.ToString();
                    myExcelSheet.Cells[3, 4] = "测试";
                    int mySumCount = myQuerys1.Count();
                    int StartRow = 6;

                    for (int i = 0; i < mySumCount; i++)
                    {
                        myExcelSheet.Cells[StartRow + i, 1] = myQuerys1[i].作业中心ID.ToString();
                        myExcelSheet.Cells[StartRow + i, 2] = myQuerys1[i].作业中心名称.ToString();
                        myExcelSheet.Cells[StartRow + i, 3] = myQuerys1[i].类型.ToString();
                        myExcelSheet.Cells[StartRow + i, 4] = myQuerys1[i].是否直接成本.ToString();
                        myExcelSheet.Cells[StartRow + i, 5] = myQuerys1[i].成本细项ID.ToString();

                    }
                }



                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }
        #endregion

        #region //作业中心成本报表

       
        public string WriteExcel_Report_ZYZXCBB(int CenterId, string PIPId,string AccountPeriod ,string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "作业中心成本表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];
            //写入报表点击查询页面
            WriteReportStatistics("作业中心成本表", UserName, CenterId.ToString() + "-" + PIPId + "-" + AccountPeriod, DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {

                var MyPIPInfos = from a in db.VABC_Report_PiPMeterialsCost
                                where a.作业中心ID == CenterId
                                && a.中间体ID == PIPId  
                                && a.会计期间ID == AccountPeriod 
                              //  && a.转入标记 == 0 
                                select new
                                {   
                                    a.作业中心ID,
                                    a.作业中心名称,
                                    a.中间体ID,
                                    a.中间体名称,
                                    a.中间体数量

                                };

   
                int StarRow = 7;
                int j = 0;
                int k = 0;
                int n = 0;

                if (MyPIPInfos.Count() == 0)
                {
                    myExcelSheet.Cells[1, 1] = "没有满足查询的材料记录";

                }
                else
                {  
                    //填入表头信息
                    var MyPIPInfo = MyPIPInfos.Take(1).Single();
                    myExcelSheet.Cells[3, 1] = "作业中心名称：(" + MyPIPInfo.作业中心ID.ToString() + ")" + MyPIPInfo.作业中心名称;
                    myExcelSheet.Cells[4, 1] = "产品中间体名称：(" + MyPIPInfo.中间体ID.ToString() + ")" + MyPIPInfo.中间体名称;
         
                    myExcelSheet.Cells[4, 4] = MyPIPInfo.中间体数量;

                    myExcelSheet.Cells[3, 4] = "会计期间：(" + AccountPeriod + ")";
                    myExcelSheet.Cells[3, 7] = "产品中间体名称：(" + MyPIPInfo.中间体ID.ToString() + ")" + MyPIPInfo.中间体名称;
                    myExcelSheet.Cells[3, 11] = "产量：(" + MyPIPInfo.中间体数量.ToString() + ")" ;

                    var MyMeterialRowsCount = from b in db.VABC_Report_PiPMeterialsCost
                                       
                                        where b.作业中心ID == CenterId
                                        && b.中间体ID == PIPId
                                        && b.会计期间ID == AccountPeriod
                                        // && b.转入标记 == 0 
                                         orderby b.材料ID,b.转入标记
                                        select b;



                    if (MyMeterialRowsCount.Count() != 0)
                    {
                        int InsertRowNum = MyMeterialRowsCount.Count();


                        Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[8, Missing];
                 
                        for (int i = 0; i < InsertRowNum ; i++)
                        {
                            myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                            //myRange.RowHeight = 11;

                        }
                        myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                        myRange.RowHeight = 11;
                        myRange = null;

                    }


                    var MyMeterialRows = from b in db.VABC_Report_PiPMeterialsCost

                                         where b.作业中心ID == CenterId
                                         && b.中间体ID == PIPId
                                         && b.会计期间ID == AccountPeriod
                                         && b.转入标记 == 0 
                                         orderby b.材料ID, b.转入标记
                                         select b;

                    foreach (var MyMeterialRow in MyMeterialRows)
                    {
                        myExcelSheet.Cells[StarRow + j, 1] = MyMeterialRow.材料ID;
                        myExcelSheet.Cells[StarRow + j, 2] = MyMeterialRow.材料名称;

                        myExcelSheet.Cells[StarRow + j, 3] = MyMeterialRow.计量单位;
                        myExcelSheet.Cells[StarRow + j, 4] = MyMeterialRow.期初数量;
                        myExcelSheet.Cells[StarRow + j, 5] = MyMeterialRow.期初金额;
                      //  myExcelSheet.Cells[StarRow + j, 6] = MyMeterialRow.转入数量;
                      //  myExcelSheet.Cells[StarRow + j, 7] = MyMeterialRow.转入金额;
                        myExcelSheet.Cells[StarRow + j, 6] = MyMeterialRow.投入数量;
                        myExcelSheet.Cells[StarRow + j, 7] = MyMeterialRow.投入金额;
                        myExcelSheet.Cells[StarRow + j, 8] = MyMeterialRow.结存数量;
                        myExcelSheet.Cells[StarRow + j, 9] = MyMeterialRow.结存金额;
                        myExcelSheet.Cells[StarRow + j, 10] = MyMeterialRow.转出数量;
                        myExcelSheet.Cells[StarRow + j, 11] = MyMeterialRow.转出金额;
                        myExcelSheet.Cells[StarRow + j, 12] = MyMeterialRow.损失数量;
                        myExcelSheet.Cells[StarRow + j, 13] = MyMeterialRow.损失金额;
            

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
                       "=if(iserror(J" + (StarRow + j) +"/D4),0," + "J" + (StarRow + j) + "/D4)";

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                            "=if(iserror(K" + (StarRow + j)+"/D4),0," + "K" + (StarRow + j) + "/D4)";
                    
                      j++;
                    }

                    var MyPIPRows = from b in db.VABC_Report_PiPMeterialsCost

                                         where b.作业中心ID == CenterId
                                         && b.中间体ID == PIPId
                                         && b.会计期间ID == AccountPeriod
                                         && b.转入标记 == 1
                                         orderby b.材料ID, b.转入标记
                                         select b;

                    foreach (var MyPIPRow in MyPIPRows)
                    {
                        myExcelSheet.Cells[StarRow + j, 1] = MyPIPRow.材料ID;
                        myExcelSheet.Cells[StarRow + j, 2] = MyPIPRow.材料名称;

                        myExcelSheet.Cells[StarRow + j, 3] = MyPIPRow.计量单位;
                        myExcelSheet.Cells[StarRow + j, 4] = MyPIPRow.期初数量;
                        myExcelSheet.Cells[StarRow + j, 5] = MyPIPRow.期初金额;
                        //  myExcelSheet.Cells[StarRow + j, 6] = MyMeterialRow.转入数量;
                        //  myExcelSheet.Cells[StarRow + j, 7] = MyMeterialRow.转入金额;
                        myExcelSheet.Cells[StarRow + j, 6] = MyPIPRow.投入数量;
                        myExcelSheet.Cells[StarRow + j, 7] = MyPIPRow.投入金额;
                        myExcelSheet.Cells[StarRow + j, 8] = MyPIPRow.结存数量;
                        myExcelSheet.Cells[StarRow + j, 9] = MyPIPRow.结存金额;
                        myExcelSheet.Cells[StarRow + j, 10] = MyPIPRow.转出数量;
                        myExcelSheet.Cells[StarRow + j, 11] = MyPIPRow.转出金额;
                        myExcelSheet.Cells[StarRow + j, 12] = MyPIPRow.损失数量;
                        myExcelSheet.Cells[StarRow + j, 13] = MyPIPRow.损失金额;


                  
                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
"=if(iserror(J" + (StarRow + j) + "/D4),0," + "J" + (StarRow + j) + "/D4)";

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                            "=if(iserror(K" + (StarRow + j) + "/D4),0," + "K" + (StarRow + j) + "/D4)";
                        j++;
                    }

                  
                }

                StarRow = 11+ j;


                    var MyActivityCostRows = from m in db.VABC_Report_PiPActiveCost 
                                             where m.作业中心ID == CenterId
                                               && m.中间体ID == PIPId
                                                 && m.会计期间ID == AccountPeriod && m.参数描述 == "DL"
                                              select m;
                string  MyAp = AccountPeriod;
                string  MyPriorAp = PriorAccountPeriod(MyAp);

                if(MyActivityCostRows.Count() == 0 )
                {
                 myExcelSheet.Cells[1, 1] = "没有满足查询的作业成本记录";

                }
                else
                {

                    int InsertRowNum = MyActivityCostRows.Count();
                    Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow+1, Missing];

                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        range1.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                        //range1.RowHeight = 11;
                    }
                    range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];
                    range1.RowHeight = 11;
                    range1 = null;

                                    //填入表头信息
                    var MyActivityCostInfo = MyActivityCostRows.Take(1).Single();



                    myExcelSheet.Cells[3, 1] = "作业中心名称：(" + MyActivityCostInfo.作业中心ID.ToString() + ")" + MyActivityCostInfo.作业中心名称;
                    myExcelSheet.Cells[4, 1] = "产品中间体名称：(" + MyActivityCostInfo.中间体ID.ToString() + ")" + MyActivityCostInfo.中间体名称;

                    myExcelSheet.Cells[4, 4] = MyActivityCostInfo.中间体数量;

                    myExcelSheet.Cells[3, 4] = "会计期间：(" + AccountPeriod + ")";
                    myExcelSheet.Cells[3, 7] = "产品中间体名称：(" + MyActivityCostInfo.中间体ID.ToString() + ")" + MyActivityCostInfo.中间体名称;
                    myExcelSheet.Cells[3, 11] = "产量：(" + MyActivityCostInfo.中间体数量.ToString() + ")";
                    foreach (var MyActivityCostRow in MyActivityCostRows)
                    {
                        string QueryDetailName = MyActivityCostRow.成本细项名称;
                        myExcelSheet.Cells[StarRow + k, 2] = QueryDetailName;
                        myExcelSheet.Cells[StarRow + k, 4] = MyActivityCostRow.期初数量;
                        myExcelSheet.Cells[StarRow + k, 5] = MyActivityCostRow.期初金额;
                        myExcelSheet.Cells[StarRow + k, 6] = MyActivityCostRow.投入数量;
                        //  myExcelSheet.Cells[StarRow, 7] = MyActivityCostRow.转入金额;
                        myExcelSheet.Cells[StarRow + k, 7] = MyActivityCostRow.投入金额;
                        myExcelSheet.Cells[StarRow + k, 8] = MyActivityCostRow.结存数量;
                        myExcelSheet.Cells[StarRow + k, 9] = MyActivityCostRow.结存金额;
                        myExcelSheet.Cells[StarRow + k, 10] = MyActivityCostRow.转出数量;
                        myExcelSheet.Cells[StarRow + k, 11] = MyActivityCostRow.转出金额;
                        myExcelSheet.Cells[StarRow + k, 12] = MyActivityCostRow.损失数量;
                        myExcelSheet.Cells[StarRow + k, 13] = MyActivityCostRow.损失金额;

                        //decimal Mount1 = (decimal)MyActivityCostRow.期初金额 / UnitPrice(MyActivityCostRow.成本细项ID, MyPriorAp);
                        //decimal Mount2 = (decimal)MyActivityCostRow.转入金额 / UnitPrice(MyActivityCostRow.成本细项ID, MyAp);
                        //decimal Mount3 = (decimal)MyActivityCostRow.投入金额 / UnitPrice(MyActivityCostRow.成本细项ID, MyAp);
                        //decimal MountSum = Mount1 + Mount2 + Mount3;
                        //decimal MoneySum = (decimal)MyActivityCostRow.期初金额 +
                                           //(decimal)MyActivityCostRow.转入金额 +
                                           //(decimal)MyActivityCostRow.投入金额;

                        //myExcelSheet.Cells[StarRow+k, 4] = Mount1;
                        //myExcelSheet.Cells[StarRow+k, 6] = Mount3;
                        //if (MoneySum != 0)
                        //{


                           // myExcelSheet.Cells[StarRow+k, 8] = ((decimal)MyActivityCostRow.结存金额 / MoneySum) * MountSum;

                           // myExcelSheet.Cells[StarRow+k, 10] = ((decimal)MyActivityCostRow.转出金额 / MoneySum) * MountSum;

                           // myExcelSheet.Cells[StarRow+k, 12] = ((decimal)MyActivityCostRow.损失金额 / MoneySum) * MountSum;

                        //}
                        int TempRow = StarRow + k;
                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow+k, 14]).Formula =
                            "=if(iserror(J" + TempRow + "/D4),0," + "J" + TempRow + "/D4)";

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow+k, 15]).Formula =
                            "=if(iserror(K" + TempRow + "/D4),0," + "K" + TempRow + "/D4)";

                        k++;
                    }


                }

                StarRow = 16+ j+k;


                var MyActivityCostRows_0 = from m in db.VABC_Report_PiPActiveCost
                                         where m.作业中心ID == CenterId
                                           && m.中间体ID == PIPId
                                             && m.会计期间ID == AccountPeriod && m.参数描述=="ZY"
                                         select m;
                if (MyActivityCostRows_0.Count() == 0)
                {
                    myExcelSheet.Cells[1, 1] = "没有满足查询的作业成本记录";

                }
                else
                {

                    int InsertRowNum = MyActivityCostRows_0.Count();
                    Microsoft.Office.Interop.Excel.Range range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow ,Missing];

                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        range2.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                        //range2.RowHeight = 11;
                    }
                    range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow+1, Missing];
                    range2.RowHeight = 11;
                    range2 = null;

                    //填入表头信息
                    var MyActivityCostInfo = MyActivityCostRows_0.Take(1).Single();



                    myExcelSheet.Cells[3, 1] = "作业中心名称：(" + MyActivityCostInfo.作业中心ID.ToString() + ")" + MyActivityCostInfo.作业中心名称;
                    myExcelSheet.Cells[4, 1] = "产品中间体名称：(" + MyActivityCostInfo.中间体ID.ToString() + ")" + MyActivityCostInfo.中间体名称;

                    myExcelSheet.Cells[4, 4] = MyActivityCostInfo.中间体数量;

                    myExcelSheet.Cells[3, 4] = "会计期间：(" + AccountPeriod + ")";
                    myExcelSheet.Cells[3, 7] = "产品中间体名称：(" + MyActivityCostInfo.中间体ID.ToString() + ")" + MyActivityCostInfo.中间体名称;
                    myExcelSheet.Cells[3, 11] = "产量：(" + MyActivityCostInfo.中间体数量.ToString() + ")";
                    foreach (var MyActivityCostRow0 in MyActivityCostRows_0)
                    {
                        string QueryDetailName = MyActivityCostRow0.成本细项名称;
                        myExcelSheet.Cells[StarRow + n, 2] = QueryDetailName;
                        myExcelSheet.Cells[StarRow + n, 4] = MyActivityCostRow0.期初数量;
                        myExcelSheet.Cells[StarRow +n, 5] = MyActivityCostRow0.期初金额;
                        //  myExcelSheet.Cells[StarRow + 7, 7] = MyActivityCostRow.转入金额;
                        myExcelSheet.Cells[StarRow + n, 6] = MyActivityCostRow0.投入数量;
                        myExcelSheet.Cells[StarRow + n, 7] = MyActivityCostRow0.投入金额;
                        myExcelSheet.Cells[StarRow + n, 8] = MyActivityCostRow0.结存数量;
                        myExcelSheet.Cells[StarRow + n, 9] = MyActivityCostRow0.结存金额;
                        myExcelSheet.Cells[StarRow + n, 10] = MyActivityCostRow0.转出数量;
                        myExcelSheet.Cells[StarRow + n, 11] = MyActivityCostRow0.转出金额;
                        myExcelSheet.Cells[StarRow + n, 12] = MyActivityCostRow0.损失数量;
                        myExcelSheet.Cells[StarRow + n, 13] = MyActivityCostRow0.损失金额;
                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + n, 14]).Formula =
"=if(iserror(J" + (StarRow + n) + "/D4),0," + "J" + (StarRow + n) + "/D4)";

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + n, 15]).Formula =
                            "=if(iserror(K" + (StarRow + n) + "/D4),0," + "K" + (StarRow + n) + "/D4)";

                       n++;



                    }


                }
                myExcelBook.Save();
                Range range = myExcelSheet.get_Range("A1".ToString(), Missing);
                Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");
                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion


        #region //作业中心成本报表_不包括中间体信息。全部还原成基本原材料


        public string WriteExcel_Report_ZYZXCBB_NoPIPInfo(int CenterId, string PIPId, string AccountPeriod, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "作业中心成本表_不含中间体信息");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];
            //写入报表点击查询页面
            WriteReportStatistics("作业中心成本表_不含中间体信息", UserName, CenterId.ToString() + "-" + PIPId + "-" + AccountPeriod, DateTime.Now);

            ClearThreeObject();



            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {

                var MyPIPInfos = from a in db.VABC_Report_PiPMeterialsCost_不含中间体信息
                                 where a.作业中心ID == CenterId
                                 && a.中间体ID == PIPId
                                 && a.会计期间ID == AccountPeriod
                                 //  && a.转入标记 == 0 
                                 select new
                                 {
                                     a.作业中心ID,
                                     a.作业中心名称,
                                     a.中间体ID,
                                     a.中间体名称,
                                     a.中间体数量

                                 };


                int StarRow = 7;
                int j = 0;
                int k = 0;
                int n = 0;

                if (MyPIPInfos.Count() == 0)
                {
                    myExcelSheet.Cells[1, 1] = "没有满足查询的材料记录";

                }
                else
                {
                    //填入表头信息
                    var MyPIPInfo = MyPIPInfos.Take(1).Single();
                    myExcelSheet.Cells[3, 1] = "作业中心名称：(" + MyPIPInfo.作业中心ID.ToString() + ")" + MyPIPInfo.作业中心名称;
                    myExcelSheet.Cells[4, 1] = "产品中间体名称：(" + MyPIPInfo.中间体ID.ToString() + ")" + MyPIPInfo.中间体名称;

                    myExcelSheet.Cells[4, 4] = MyPIPInfo.中间体数量;

                    myExcelSheet.Cells[3, 4] = "会计期间：(" + AccountPeriod + ")";
                    myExcelSheet.Cells[3, 7] = "产品中间体名称：(" + MyPIPInfo.中间体ID.ToString() + ")" + MyPIPInfo.中间体名称;
                    myExcelSheet.Cells[3, 11] = "产量：(" + MyPIPInfo.中间体数量.ToString() + ")";

                    var MyMeterialRowsCount = from b in db.VABC_Report_PiPMeterialsCost_不含中间体信息
                                              where b.作业中心ID == CenterId
                                              && b.中间体ID == PIPId
                                              && b.会计期间ID == AccountPeriod
                                              // && b.转入标记 == 0 
                                              orderby b.材料ID
                                              select b;



                    if (MyMeterialRowsCount.Count() != 0)
                    {
                        int InsertRowNum = MyMeterialRowsCount.Count();


                        Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[8, Missing];

                        for (int i = 0; i < InsertRowNum; i++)
                        {
                            myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                            //myRange.RowHeight = 11;

                        }
                        myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                        myRange.RowHeight = 11;
                        myRange = null;

                    }


                    var MyMeterialRows = from b in db.VABC_Report_PiPMeterialsCost_不含中间体信息

                                         where b.作业中心ID == CenterId
                                         && b.中间体ID == PIPId
                                         && b.会计期间ID == AccountPeriod
                                         orderby b.材料ID
                                         select b;

                    foreach (var MyMeterialRow in MyMeterialRows)
                    {
                        myExcelSheet.Cells[StarRow + j, 1] = MyMeterialRow.材料ID;
                        myExcelSheet.Cells[StarRow + j, 2] = MyMeterialRow.材料名称;

                        myExcelSheet.Cells[StarRow + j, 3] = MyMeterialRow.计量单位;
                        myExcelSheet.Cells[StarRow + j, 4] = MyMeterialRow.期初数量;
                        myExcelSheet.Cells[StarRow + j, 5] = MyMeterialRow.期初金额;
                        //  myExcelSheet.Cells[StarRow + j, 6] = MyMeterialRow.转入数量;
                        //  myExcelSheet.Cells[StarRow + j, 7] = MyMeterialRow.转入金额;
                        myExcelSheet.Cells[StarRow + j, 6] = MyMeterialRow.投入数量 + MyMeterialRow.转入数量;
                        myExcelSheet.Cells[StarRow + j, 7] = MyMeterialRow.投入金额 + MyMeterialRow.转入金额;
                        myExcelSheet.Cells[StarRow + j, 8] = MyMeterialRow.结存数量;
                        myExcelSheet.Cells[StarRow + j, 9] = MyMeterialRow.结存金额;
                        myExcelSheet.Cells[StarRow + j, 10] = MyMeterialRow.转出数量;
                        myExcelSheet.Cells[StarRow + j, 11] = MyMeterialRow.转出金额;
                        myExcelSheet.Cells[StarRow + j, 12] = MyMeterialRow.损失数量;
                        myExcelSheet.Cells[StarRow + j, 13] = MyMeterialRow.损失金额;


                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
                       "=if(iserror(J" + (StarRow + j) + "/D4),0," + "J" + (StarRow + j) + "/D4)";

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                            "=if(iserror(K" + (StarRow + j) + "/D4),0," + "K" + (StarRow + j) + "/D4)";

                        j++;
                    }

                }

                StarRow = 11 + j;


                var MyActivityCostRows = from m in db.VABC_Report_PiPActiveCost_不含中间体信息
                                         where m.作业中心ID == CenterId
                                           && m.中间体ID == PIPId
                                             && m.会计期间ID == AccountPeriod 
                                         select m;
                string MyAp = AccountPeriod;
                string MyPriorAp = PriorAccountPeriod(MyAp);
                if (MyActivityCostRows.Count() == 0)
                {
                    myExcelSheet.Cells[1, 1] = "没有满足查询的作业成本记录";

                }
                else
                {
                    var MyActivityCostRows_DL = from m in db.VABC_Report_PiPActiveCost_不含中间体信息
                                             join b in db.TABC_DM_CostItems on m.成本细项ID equals b.成本细项ID
                                             where m.作业中心ID == CenterId
                                               && m.中间体ID == PIPId
                                                 && m.会计期间ID == AccountPeriod && b.参数描述 == "DL"
                                             select new
                                             {
                                                 m.作业中心ID,
                                                 m.作业中心名称,
                                                 m.产品ID,
                                                 m.产品名称,
                                                 m.成本细项ID,
                                                 m.成本细项名称,
                                                 m.会计期间ID,
                                                 m.结存金额,
                                                m.结存数量,
                                                m.期初金额,
                                                m.期初数量,
                                                m.损失金额,
                                                m.损失数量,
                                                投入金额 = m.投入金额 + m.转入金额,
                                                投入数量 = m.投入数量 + m.转入数量,
                                                m.中间体ID,
                                                m.中间体名称,
                                                m.中间体数量,
                                                转入数量 = 0,
                                                转入金额 = 0,
                                                b.报表顺序码

                                             };
                    int InsertRowNum = MyActivityCostRows_DL.Count();
                    Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow + 1, Missing];

                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        range1.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                        //range1.RowHeight = 11;
                    }
                    range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];
                    range1.RowHeight = 11;
                    range1 = null;

                    //填入表头信息
                    var MyActivityCostInfo = MyActivityCostRows.Take(1).Single();



                    myExcelSheet.Cells[3, 1] = "作业中心名称：(" + MyActivityCostInfo.作业中心ID.ToString() + ")" + MyActivityCostInfo.作业中心名称;
                    myExcelSheet.Cells[4, 1] = "产品中间体名称：(" + MyActivityCostInfo.中间体ID.ToString() + ")" + MyActivityCostInfo.中间体名称;

                    myExcelSheet.Cells[4, 4] = MyActivityCostInfo.中间体数量;

                    myExcelSheet.Cells[3, 4] = "会计期间：(" + AccountPeriod + ")";
                    myExcelSheet.Cells[3, 7] = "产品中间体名称：(" + MyActivityCostInfo.中间体ID.ToString() + ")" + MyActivityCostInfo.中间体名称;
                    myExcelSheet.Cells[3, 11] = "产量：(" + MyActivityCostInfo.中间体数量.ToString() + ")";
                    foreach (var MyActivityCostRow in MyActivityCostRows_DL)
                    {
                        string QueryDetailName = MyActivityCostRow.成本细项名称;
                        myExcelSheet.Cells[StarRow + k, 2] = QueryDetailName;
                        myExcelSheet.Cells[StarRow + k, 4] = MyActivityCostRow.期初数量;
                        myExcelSheet.Cells[StarRow + k, 5] = MyActivityCostRow.期初金额;
                        //  myExcelSheet.Cells[StarRow, 7] = MyActivityCostRow.转入金额;
                        myExcelSheet.Cells[StarRow + k, 6] = MyActivityCostRow.投入数量;
                        myExcelSheet.Cells[StarRow + k, 7] = MyActivityCostRow.投入金额;
                        myExcelSheet.Cells[StarRow + k, 8] = MyActivityCostRow.结存数量;
                        myExcelSheet.Cells[StarRow + k, 9] = MyActivityCostRow.结存金额;
                        myExcelSheet.Cells[StarRow + k, 10] = MyActivityCostRow.转入数量;
                        myExcelSheet.Cells[StarRow + k, 11] = MyActivityCostRow.转入金额;
                        myExcelSheet.Cells[StarRow + k, 12] = MyActivityCostRow.损失数量;
                        myExcelSheet.Cells[StarRow + k, 13] = MyActivityCostRow.损失金额;
                        int TempRow = StarRow + k;
                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + k, 14]).Formula =
                            "=if(iserror(J" + TempRow + "/D4),0," + "J" + TempRow + "/D4)";

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + k, 15]).Formula =
                            "=if(iserror(K" + TempRow + "/D4),0," + "K" + TempRow + "/D4)";

                        k++;
                    }


                }

                StarRow = 16 + j + k;


                var MyActivityCostRows_ZY = from m in db.VABC_Report_PiPActiveCost_不含中间体信息
                                            join b in db.TABC_DM_CostItems on m.成本细项ID equals b.成本细项ID
                                            where m.作业中心ID == CenterId
                                              && m.中间体ID == PIPId
                                                && m.会计期间ID == AccountPeriod && b.参数描述 == "ZY"
                                            select new
                                            {
                                                m.作业中心ID,
                                                m.作业中心名称,
                                                m.产品ID,
                                                m.产品名称,
                                                m.成本细项ID,
                                                m.成本细项名称,
                                                m.会计期间ID,
                                                m.结存金额,
                                                m.结存数量,
                                                m.期初金额,
                                                m.期初数量,
                                                m.损失金额,
                                                m.损失数量,
                                                投入金额 = m.投入金额 + m.转入金额,
                                                投入数量 = m.投入数量 + m.转入数量,
                                                m.中间体ID,
                                                m.中间体名称,
                                                m.中间体数量,
                                                转入数量 = 0,
                                                转入金额 = 0,
                                                b.报表顺序码

                                            };
                int InsertRowNum2 = MyActivityCostRows_ZY.Count();
                   
                    Microsoft.Office.Interop.Excel.Range range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];

                    for (int i = 0; i < InsertRowNum2; i++)
                    {
                        range2.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                        //range2.RowHeight = 11;
                    }
                    range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow-1, Missing];
                    range2.RowHeight = 11;
                    range2 = null;
                    foreach (var MyActivityCostRow0 in MyActivityCostRows_ZY)
                    {
                        string QueryDetailName = MyActivityCostRow0.成本细项名称;
                        myExcelSheet.Cells[StarRow + n, 2] = QueryDetailName;
                        myExcelSheet.Cells[StarRow + n, 4] = MyActivityCostRow0.期初数量;
                        myExcelSheet.Cells[StarRow + n, 5] = MyActivityCostRow0.期初金额;
                        //  myExcelSheet.Cells[StarRow, 7] = MyActivityCostRow0.转入金额;
                        myExcelSheet.Cells[StarRow + n, 6] = MyActivityCostRow0.投入数量;
                        myExcelSheet.Cells[StarRow + n, 7] = MyActivityCostRow0.投入金额;
                        myExcelSheet.Cells[StarRow + n, 8] = MyActivityCostRow0.结存数量;
                        myExcelSheet.Cells[StarRow + n, 9] = MyActivityCostRow0.结存金额;
                        myExcelSheet.Cells[StarRow + n, 10] = MyActivityCostRow0.转入数量;
                        myExcelSheet.Cells[StarRow + n, 11] = MyActivityCostRow0.转入金额;
                        myExcelSheet.Cells[StarRow + n, 12] = MyActivityCostRow0.损失数量;
                        myExcelSheet.Cells[StarRow + n, 13] = MyActivityCostRow0.损失金额;
                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + n, 14]).Formula =
"=if(iserror(J" + (StarRow + n) + "/D4),0," + "J" + (StarRow + n) + "/D4)";

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + n, 15]).Formula =
                            "=if(iserror(K" + (StarRow + n) + "/D4),0," + "K" + (StarRow + n) + "/D4)";

                        n++;



                 


                }
                myExcelBook.Save();
                Range range = myExcelSheet.get_Range("A1".ToString(), Missing);
                Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");
                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion


        #region //作业中心成本变动分析表


        public string WriteExcel_Report_ZYZXCBBDFXB(int CenterId, string PIPId, string Ap1, string Ap2,string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "作业中心成本变动分析表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];

            //写入报表点击查询页面
            WriteReportStatistics("作业中心成本表", UserName, CenterId.ToString() + "-" + PIPId + "-" + Ap1 + "-" + Ap2, DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {


                var MyMeterialRows = (from a in db.VABC_Report_PiPMeterialsCost
                                      where a.作业中心ID == CenterId
                                      && a.中间体ID == PIPId
                                      && new string[] { Ap1, Ap2 }.Contains(a.会计期间ID)
                                      orderby a.材料ID
                                      select a.材料ID).Distinct();


                int StarRow = 6;
                int j = 0;

                if (MyMeterialRows.Count() == 0)
                {
                    myExcelSheet.Cells[1, 1] = "没有满足查询的材料记录";

                }
                else
                {

                        int InsertRowNum = MyMeterialRows.Count();


                        Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                        for (int i = 0; i < InsertRowNum; i++)
                        {
                            myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                        }
                        myRange = null;

             




                    foreach (var MyMeterialRow in MyMeterialRows)
                    {
                        var MyPIPMeterialQuerys = from a in db.VABC_Report_PiPMeterialsCost
                                      where a.作业中心ID == CenterId
                                      && a.中间体ID == PIPId
                                       && a.会计期间ID == Ap1
                                       && a.材料ID == MyMeterialRow.ToString()
                                      select a;

                        if (MyPIPMeterialQuerys.Count() == 1)
                        {

                            var MyPIPMeterialQuery = MyPIPMeterialQuerys.Single();
                            myExcelSheet.Cells[2, 1] = "作业中心名称：(" + MyPIPMeterialQuery.作业中心ID.ToString() + ")" + MyPIPMeterialQuery.作业中心名称;
                            myExcelSheet.Cells[3, 1] = "产品中间体名称：(" + MyPIPMeterialQuery.中间体ID.ToString() + ")" + MyPIPMeterialQuery.中间体名称;

                            myExcelSheet.Cells[StarRow + j, 1] = MyPIPMeterialQuery.材料ID;
                            myExcelSheet.Cells[StarRow + j, 2] = MyPIPMeterialQuery.材料名称;
                            myExcelSheet.Cells[StarRow + j, 3] = MyPIPMeterialQuery.计量单位;
                            if ( MyPIPMeterialQuery.中间体数量 != 0)
                            {
                                decimal MyUnitMount = (decimal)MyPIPMeterialQuery.转出数量 / (decimal)MyPIPMeterialQuery.中间体数量;
                                myExcelSheet.Cells[StarRow + j, 4] = MyUnitMount;
                                decimal MyUnitMoney = (decimal)MyPIPMeterialQuery.转出金额 / (decimal)MyPIPMeterialQuery.中间体数量;
                                myExcelSheet.Cells[StarRow + j, 5] = MyUnitMoney;
                            }


                   

                        }
                                    var MyPIPMeterialQuery2s = from a in db.VABC_Report_PiPMeterialsCost
                                                              where a.作业中心ID == CenterId
                                                              && a.中间体ID == PIPId
                                                               && a.会计期间ID == Ap2
                                                               && a.材料ID == MyMeterialRow.ToString()
                                                              select a;

                                    if(MyPIPMeterialQuery2s.Count() == 1)
                                    {

                                        var MyPIPMeterialQuery2 = MyPIPMeterialQuery2s.Single();
                                    myExcelSheet.Cells[2, 1] = "作业中心名称：(" + MyPIPMeterialQuery2.作业中心ID.ToString() + ")" + MyPIPMeterialQuery2.作业中心名称;
                                    myExcelSheet.Cells[3, 1] = "产品中间体名称：(" + MyPIPMeterialQuery2.中间体ID.ToString() + ")" + MyPIPMeterialQuery2.中间体名称;
                        

                                    myExcelSheet.Cells[StarRow + j, 1] = MyPIPMeterialQuery2.材料ID;
                                    myExcelSheet.Cells[StarRow + j, 2] = MyPIPMeterialQuery2.材料名称;
                                    myExcelSheet.Cells[StarRow + j, 3] = MyPIPMeterialQuery2.计量单位;
                                   
                                        if (MyPIPMeterialQuery2.中间体数量 != 0 )
                                  
                                        {
                                        decimal MyUnitMount = (decimal)MyPIPMeterialQuery2.转出数量 / (decimal)MyPIPMeterialQuery2.中间体数量;
                                        myExcelSheet.Cells[StarRow + j, 6] = MyUnitMount;
                                        decimal MyUnitMoney = (decimal)MyPIPMeterialQuery2.转出金额 / (decimal)MyPIPMeterialQuery2.中间体数量;
                                        myExcelSheet.Cells[StarRow + j, 7] = MyUnitMoney;
                                   
                                        }                                                                    
                       
                        
                                        }

                                    ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 8]).Formula =

                                   "=" + "E" + (StarRow + j) + "-" + "G" + (StarRow + j);
                                    ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 9]).Formula =

                                                        "=if(iserror(" + "H" + (StarRow + j) + "/G" + (StarRow + j) + "),0," + "H" + (StarRow + j) + "/G" + (StarRow + j) + ")";
                                    
                        j++;
                    }


                }

                StarRow = 10 + j;


                var MyActivityCostQuerys = from m in db.VABC_Report_PiPActiveCost

                                         where m.作业中心ID == CenterId
                                           && m.中间体ID == PIPId
                                           && new string[] { Ap1, Ap2 }.Contains(m.会计期间ID)
                                           orderby m.成本细项ID
                                         select m;

                if (MyActivityCostQuerys.Count() == 0)
                {
                    myExcelSheet.Cells[1, 1] = "没有满足查询的作业成本记录";

                }
                else
                {


                    //填入表头信息
                    var MyActivityCostInfo = MyActivityCostQuerys.Take(1).Single();
                    myExcelSheet.Cells[2, 1] = "作业中心名称：(" + MyActivityCostInfo.作业中心ID.ToString() + ")" + MyActivityCostInfo.作业中心名称;
                    myExcelSheet.Cells[3, 1] = "产品中间体名称：(" + MyActivityCostInfo.中间体ID.ToString() + ")" + MyActivityCostInfo.中间体名称;


                    var MyActivityCostQuery_DLS = from m in db.VABC_Report_PiPActiveCost
                                               join b in db.TABC_DM_CostItems on m.成本细项ID equals b.成本细项ID
                                               where m.作业中心ID == CenterId
                                                 && m.中间体ID == PIPId
                                                 && new string[] { Ap1, Ap2 }.Contains(m.会计期间ID) && b.参数描述 == "DL"
                                               orderby b.报表顺序码
                                               select m;
                    var MyActivityCostQuery_DL = from a in MyActivityCostQuery_DLS orderby a.成本细项ID select a;
                    var MyActivityCostQuery_ZYS = from m in db.VABC_Report_PiPActiveCost
                                                 join b in db.TABC_DM_CostItems on m.成本细项ID equals b.成本细项ID
                                                 where m.作业中心ID == CenterId
                                                   && m.中间体ID == PIPId
                                                   && new string[] { Ap1, Ap2 }.Contains(m.会计期间ID) && b.参数描述 == "ZY"
                                                 orderby b.报表顺序码
                                                 select m;
                    var MyActivityCostQuery_ZY = from a in MyActivityCostQuery_ZYS orderby a.成本细项ID select a;
                    //Range range = myExcelSheet.get_Range(("A" + (StarRow + 20).ToString()).ToString(), Missing);
                    //Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");
                    int InsertRowNum = MyActivityCostQuery_DLS.Count()/2;
                    int InsertRowNum2 = MyActivityCostQuery_ZYS.Count()/2;
                    Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow + 1, Missing];

                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        range1.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                        //range1.RowHeight = 11;
                    }
                    range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];
                    range1.RowHeight = 11;
                    range1 = null;

                    j = 0;
                    bool flag = true;
                    foreach (var MyActivityCostQuery in MyActivityCostQuery_DL)
                    {
                        myExcelSheet.Cells[StarRow + j, 2] = MyActivityCostQuery.成本细项名称;
                        if (MyActivityCostQuery.会计期间ID.ToString() == Ap1)
                        {

                            
                            if (MyActivityCostQuery.中间体数量 != 0)
                            {
                                decimal MyUnitMoney = (decimal)MyActivityCostQuery.转出金额 / (decimal)MyActivityCostQuery.中间体数量;
                                myExcelSheet.Cells[StarRow + j, 5] = MyUnitMoney;
                            }
                            if (flag == true)
                            {
                                flag = false;
                            }
                            else
                            {
                                flag = true;
                            }

                        }
                        else if (MyActivityCostQuery.会计期间ID.ToString() == Ap2)
                        {
                            if (MyActivityCostQuery.中间体数量 != 0)
                            {

                                decimal MyUnitMoney = (decimal)MyActivityCostQuery.转出金额 / (decimal)MyActivityCostQuery.中间体数量;
                                myExcelSheet.Cells[StarRow + j, 7] = MyUnitMoney;
                            }
                            if (flag == true)
                            {
                                flag = false;
                            }
                            else
                            {
                                flag = true;
                            }


                        }
                        if (flag == false)
                        {
                        }
                        else
                        {
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 8]).Formula =
                                            "=" + "E" + (StarRow + j) + "-" + "G" + (StarRow + j);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 9]).Formula =
                                "=if(iserror(" + "H" + (StarRow + j) + "/G" + (StarRow + j) + "),0," + "H" + (StarRow + j) + "/G" + (StarRow + 9) + ")";
                            j++;
                        }
                    }
                    StarRow = StarRow + j+4;
                    Microsoft.Office.Interop.Excel.Range range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow + 1, Missing];
                    
                    for (int i = 0; i < InsertRowNum2; i++)
                    {
                        range2.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                        //range1.RowHeight = 11;
                    }
                    range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];
                    range2.RowHeight = 11;
                    range2 = null;
                    j = 0;
                    flag = true;
                    foreach (var MyActivityCostQuery in MyActivityCostQuery_ZY)
                    {
                        myExcelSheet.Cells[StarRow + j, 2] = MyActivityCostQuery.成本细项名称;
                        if (MyActivityCostQuery.会计期间ID.ToString() == Ap1)
                        {
                            if (MyActivityCostQuery.中间体数量 != 0)
                            {
                                decimal MyUnitMoney = (decimal)MyActivityCostQuery.转出金额 / (decimal)MyActivityCostQuery.中间体数量;
                                myExcelSheet.Cells[StarRow + j, 5] = MyUnitMoney;
                            }
                            if (flag == true)
                            {
                                flag = false;
                            }
                            else
                            {
                                flag = true;
                            }

                        }
                        else if (MyActivityCostQuery.会计期间ID.ToString() == Ap2)
                        {
                            
                            if (MyActivityCostQuery.中间体数量 != 0)
                            {

                                decimal MyUnitMoney = (decimal)MyActivityCostQuery.转出金额 / (decimal)MyActivityCostQuery.中间体数量;
                                myExcelSheet.Cells[StarRow + j, 7] = MyUnitMoney;
                            }
                            if (flag == true)
                            {
                                flag = false;
                            }
                            else
                            {
                                flag = true;
                            }



                        };
                        if (flag == false)
                        {
                        }
                        else
                        {
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 8]).Formula =
                                            "=" + "E" + (StarRow + j) + "-" + "G" + (StarRow + j);
                            ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 9]).Formula =
                                "=if(iserror(" + "H" + (StarRow + j) + "/G" + (StarRow + j) + "),0," + "H" + (StarRow + j) + "/G" + (StarRow + 9) + ")";

                            j++;
                        }

                    }


                }


                myExcelBook.Save();
                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion


        #region //作业中心计划成本报表


        public string WriteExcel_Report_ZYZXJHCBB(int CenterId, string PIPId, string AccountPeriod, string UserName)
        {

            string[] Myfile = ReturnfilePath(UserName, "作业中心计划成本表");
            string StrSamepleFile = Myfile[0];
            string StrReportName = Myfile[1];
            string StrReportFile = Myfile[2];
            string StrReportFileHtml = Myfile[3];
            //写入报表点击查询页面
            WriteReportStatistics("作业中心计划成本表", UserName, CenterId.ToString() + "-" + PIPId + "-" + AccountPeriod, DateTime.Now);

            ClearThreeObject();


            object Missing = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            myExcelApp.Workbooks._Open(StrReportFile, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            myExcelBook = myExcelApp.Workbooks[1];
            myExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelBook.ActiveSheet;



            try
            {

                var MyPIPInfos = from a in db.VABC_Report_PiPMeterialsCost_Plan
                                 where a.作业中心ID == CenterId
                                 && a.中间体ID == PIPId
                                 && a.会计期间ID == AccountPeriod
                                 //  && a.转入标记 == 0 
                                 select new
                                 {
                                     a.作业中心ID,
                                     a.作业中心名称,
                                     a.中间体ID,
                                     a.中间体名称,
                                     a.中间体数量

                                 };


                int StarRow = 7;
                int j = 0;
                int k = 0;
                int n = 0;

                if (MyPIPInfos.Count() == 0)
                {
                    myExcelSheet.Cells[1, 1] = "没有满足查询的材料记录";

                }
                else
                {
                    //填入表头信息
                    var MyPIPInfo = MyPIPInfos.Take(1).Single();
                    myExcelSheet.Cells[3, 1] = "作业中心名称：(" + MyPIPInfo.作业中心ID.ToString() + ")" + MyPIPInfo.作业中心名称;
                    myExcelSheet.Cells[4, 1] = "产品中间体名称：(" + MyPIPInfo.中间体ID.ToString() + ")" + MyPIPInfo.中间体名称;

                    myExcelSheet.Cells[4, 4] = MyPIPInfo.中间体数量;

                    myExcelSheet.Cells[3, 4] = "会计期间：(" + AccountPeriod + ")";
                    myExcelSheet.Cells[3, 7] = "产品中间体名称：(" + MyPIPInfo.中间体ID.ToString() + ")" + MyPIPInfo.中间体名称;
                    myExcelSheet.Cells[3, 11] = "产量：(" + MyPIPInfo.中间体数量.ToString() + ")";

                    var MyMeterialRowsCount = from b in db.VABC_Report_PiPMeterialsCost_Plan

                                              where b.作业中心ID == CenterId
                                              && b.中间体ID == PIPId
                                              && b.会计期间ID == AccountPeriod
                                              // && b.转入标记 == 0 
                                              orderby b.材料ID, b.转入标记
                                              select b;



                    if (MyMeterialRowsCount.Count() != 0)
                    {
                        int InsertRowNum = MyMeterialRowsCount.Count();


                        Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[8, Missing];

                        for (int i = 0; i < InsertRowNum; i++)
                        {
                            myRange.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                            //myRange.RowHeight = 11;

                        }
                        myRange = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[7, Missing];
                        myRange.RowHeight = 11;
                        myRange = null;

                    }


                    var MyMeterialRows = from b in db.VABC_Report_PiPMeterialsCost_Plan

                                         where b.作业中心ID == CenterId
                                         && b.中间体ID == PIPId
                                         && b.会计期间ID == AccountPeriod
                                         && b.转入标记 == 0
                                         orderby b.材料ID, b.转入标记
                                         select b;

                    foreach (var MyMeterialRow in MyMeterialRows)
                    {
                        myExcelSheet.Cells[StarRow + j, 1] = MyMeterialRow.材料ID;
                        myExcelSheet.Cells[StarRow + j, 2] = MyMeterialRow.材料名称;

                        myExcelSheet.Cells[StarRow + j, 3] = MyMeterialRow.计量单位;
                        myExcelSheet.Cells[StarRow + j, 4] = MyMeterialRow.期初数量;
                        myExcelSheet.Cells[StarRow + j, 5] = MyMeterialRow.期初金额;
                        //  myExcelSheet.Cells[StarRow + j, 6] = MyMeterialRow.转入数量;
                        //  myExcelSheet.Cells[StarRow + j, 7] = MyMeterialRow.转入金额;
                        myExcelSheet.Cells[StarRow + j, 6] = MyMeterialRow.投入数量;
                        myExcelSheet.Cells[StarRow + j, 7] = MyMeterialRow.投入金额;
                        myExcelSheet.Cells[StarRow + j, 8] = MyMeterialRow.结存数量;
                        myExcelSheet.Cells[StarRow + j, 9] = MyMeterialRow.结存金额;
                        myExcelSheet.Cells[StarRow + j, 10] = MyMeterialRow.转出数量;
                        myExcelSheet.Cells[StarRow + j, 11] = MyMeterialRow.转出金额;
                        myExcelSheet.Cells[StarRow + j, 12] = MyMeterialRow.损失数量;
                        myExcelSheet.Cells[StarRow + j, 13] = MyMeterialRow.损失金额;


                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
                       "=if(iserror(J" + (StarRow + j) + "/D4),0," + "J" + (StarRow + j) + "/D4)";

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                            "=if(iserror(K" + (StarRow + j) + "/D4),0," + "K" + (StarRow + j) + "/D4)";

                        j++;
                    }

                    var MyPIPRows = from b in db.VABC_Report_PiPMeterialsCost_Plan

                                    where b.作业中心ID == CenterId
                                    && b.中间体ID == PIPId
                                    && b.会计期间ID == AccountPeriod
                                    && b.转入标记 == 1
                                    orderby b.材料ID, b.转入标记
                                    select b;

                    foreach (var MyPIPRow in MyPIPRows)
                    {
                        myExcelSheet.Cells[StarRow + j, 1] = MyPIPRow.材料ID;
                        myExcelSheet.Cells[StarRow + j, 2] = MyPIPRow.材料名称;

                        myExcelSheet.Cells[StarRow + j, 3] = MyPIPRow.计量单位;
                        myExcelSheet.Cells[StarRow + j, 4] = MyPIPRow.期初数量;
                        myExcelSheet.Cells[StarRow + j, 5] = MyPIPRow.期初金额;
                        //  myExcelSheet.Cells[StarRow + j, 6] = MyMeterialRow.转入数量;
                        //  myExcelSheet.Cells[StarRow + j, 7] = MyMeterialRow.转入金额;
                        myExcelSheet.Cells[StarRow + j, 6] = MyPIPRow.投入数量;
                        myExcelSheet.Cells[StarRow + j, 7] = MyPIPRow.投入金额;
                        myExcelSheet.Cells[StarRow + j, 8] = MyPIPRow.结存数量;
                        myExcelSheet.Cells[StarRow + j, 9] = MyPIPRow.结存金额;
                        myExcelSheet.Cells[StarRow + j, 10] = MyPIPRow.转出数量;
                        myExcelSheet.Cells[StarRow + j, 11] = MyPIPRow.转出金额;
                        myExcelSheet.Cells[StarRow + j, 12] = MyPIPRow.损失数量;
                        myExcelSheet.Cells[StarRow + j, 13] = MyPIPRow.损失金额;



                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 14]).Formula =
"=if(iserror(J" + (StarRow + j) + "/D4),0," + "J" + (StarRow + j) + "/D4)";

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + j, 15]).Formula =
                            "=if(iserror(K" + (StarRow + j) + "/D4),0," + "K" + (StarRow + j) + "/D4)";
                        j++;
                    }


                }

                StarRow = 11 + j;


                var MyActivityCostRows = from m in db.VABC_Report_PiPActiveCost
                                         where m.作业中心ID == CenterId
                                           && m.中间体ID == PIPId
                                             && m.会计期间ID == AccountPeriod
                                         select m;
                string MyAp = AccountPeriod;
                string MyPriorAp = PriorAccountPeriod(MyAp);

                if (MyActivityCostRows.Count() == 0)
                {
                    myExcelSheet.Cells[1, 1] = "没有满足查询的作业成本记录";

                }
                else
                {
                    var MyActivityCostRows_1 = from m in db.VABC_Report_PiPActiveCost
                                               where m.作业中心ID == CenterId
                                                 && m.中间体ID == PIPId
                                                   && m.会计期间ID == AccountPeriod && m.参数描述 == "DL"
                                               select m;
                    int InsertRowNum = MyActivityCostRows_1.Count();
                    Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow + 1, Missing];

                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        range1.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                        //range1.RowHeight = 11;
                    }
                    range1 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];
                    range1.RowHeight = 11;
                    range1 = null;

                    //填入表头信息
                    var MyActivityCostInfo = MyActivityCostRows.Take(1).Single();



                    myExcelSheet.Cells[3, 1] = "作业中心名称：(" + MyActivityCostInfo.作业中心ID.ToString() + ")" + MyActivityCostInfo.作业中心名称;
                    myExcelSheet.Cells[4, 1] = "产品中间体名称：(" + MyActivityCostInfo.中间体ID.ToString() + ")" + MyActivityCostInfo.中间体名称;

                    myExcelSheet.Cells[4, 4] = MyActivityCostInfo.中间体数量;

                    myExcelSheet.Cells[3, 4] = "会计期间：(" + AccountPeriod + ")";
                    myExcelSheet.Cells[3, 7] = "产品中间体名称：(" + MyActivityCostInfo.中间体ID.ToString() + ")" + MyActivityCostInfo.中间体名称;
                    myExcelSheet.Cells[3, 11] = "产量：(" + MyActivityCostInfo.中间体数量.ToString() + ")";
                    foreach (var MyActivityCostRow in MyActivityCostRows_1)
                    {
                        string QueryDetailName = MyActivityCostRow.成本细项名称;
                        myExcelSheet.Cells[StarRow + k, 2] = QueryDetailName;
                        myExcelSheet.Cells[StarRow + k, 4] = MyActivityCostRow.期初数量;
                        myExcelSheet.Cells[StarRow + k, 5] = MyActivityCostRow.期初金额;
                        myExcelSheet.Cells[StarRow + k, 6] = MyActivityCostRow.投入数量;
                        //  myExcelSheet.Cells[StarRow, 7] = MyActivityCostRow.转入金额;
                        myExcelSheet.Cells[StarRow + k, 7] = MyActivityCostRow.投入金额;
                        myExcelSheet.Cells[StarRow + k, 8] = MyActivityCostRow.结存数量;
                        myExcelSheet.Cells[StarRow + k, 9] = MyActivityCostRow.结存金额;
                        myExcelSheet.Cells[StarRow + k, 10] = MyActivityCostRow.转出数量;
                        myExcelSheet.Cells[StarRow + k, 11] = MyActivityCostRow.转出金额;
                        myExcelSheet.Cells[StarRow + k, 12] = MyActivityCostRow.损失数量;
                        myExcelSheet.Cells[StarRow + k, 13] = MyActivityCostRow.损失金额;
                        int TempRow = StarRow + k;
                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + k, 14]).Formula =
                            "=if(iserror(J" + TempRow + "/D4),0," + "J" + TempRow + "/D4)";

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + k, 15]).Formula =
                            "=if(iserror(K" + TempRow + "/D4),0," + "K" + TempRow + "/D4)";

                        k++;
                    }

                    StarRow = 16 + j + k;


                    var MyActivityCostRows_0 = from m in db.VABC_Report_PiPActiveCost
                                               where m.作业中心ID == CenterId
                                                 && m.中间体ID == PIPId
                                                   && m.会计期间ID == AccountPeriod && m.参数描述 == "ZY"
                                               select m;
                    InsertRowNum = MyActivityCostRows_0.Count();
                    Microsoft.Office.Interop.Excel.Range range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow, Missing];

                    for (int i = 0; i < InsertRowNum; i++)
                    {
                        range2.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, Missing);
                        //range2.RowHeight = 11;
                    }
                    range2 = (Microsoft.Office.Interop.Excel.Range)myExcelSheet.Rows[StarRow + 1, Missing];
                    range2.RowHeight = 11;
                    range2 = null;
                    foreach (var MyActivityCostRow0 in MyActivityCostRows_0)
                    {
                        string QueryDetailName = MyActivityCostRow0.成本细项名称;
                        myExcelSheet.Cells[StarRow + n, 2] = QueryDetailName;
                        myExcelSheet.Cells[StarRow + n, 5] = MyActivityCostRow0.期初金额;
                        //  myExcelSheet.Cells[StarRow + 7, 7] = MyActivityCostRow.转入金额;
                        myExcelSheet.Cells[StarRow + n, 7] = MyActivityCostRow0.投入金额;
                        myExcelSheet.Cells[StarRow + n, 9] = MyActivityCostRow0.结存金额;
                        myExcelSheet.Cells[StarRow + n, 11] = MyActivityCostRow0.转出金额;
                        myExcelSheet.Cells[StarRow + n, 13] = MyActivityCostRow0.损失金额;
                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + n, 14]).Formula =
"=if(iserror(J" + (StarRow + n) + "/D4),0," + "J" + (StarRow + n) + "/D4)";

                        ((Microsoft.Office.Interop.Excel.Range)myExcelSheet.Cells[StarRow + n, 15]).Formula =
                            "=if(iserror(K" + (StarRow + n) + "/D4),0," + "K" + (StarRow + n) + "/D4)";

                        n++;



                    }
                }
                myExcelBook.Save();
                Range range = myExcelSheet.get_Range("A1".ToString(), Missing);
                Hyperlink hyperlink = (Hyperlink)range.Hyperlinks.Add(range, @"javascript:history.back();", Missing, Missing, "返回");
                ExcelConvertToHtml(myExcelBook, StrReportFileHtml);

                return StrReportName;

            }
            catch
            {
                return "-1";

            }
            finally
            {
                myExcelBook.Save();
                myExcelBook.Close(true, StrReportFile, true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp);

                ClearThreeObject();
            }


        }

        #endregion
    }
}
